React + Firebase数据存储疑问:为何生成哈希ID?
Why Firebase Adds a Hash ID to Your Matches Path
Hey there! The reason you're seeing that random hash ID (like CRAZ...) in your Firebase path is totally intentional—it's how the push() method is designed to work.
Let me break it down for you:
- Firebase's
push()method exists specifically to automatically generate a unique, random child ID every time you call it. This is a built-in feature to prevent write conflicts when multiple users might be adding data to the same node simultaneously. - In your code, you’re calling
push(matches)on the/users/${user}/matchesreference. So Firebase creates a new child node undermatcheswith that random hash ID, then stores yourmatchesdata (containinggroupsandknockoutfields) inside that child node. That’s exactly why your path ends up looking like/users/user/matches/CRAZ....
Fix: Skip the Random ID with set()
If you want to store your matches data directly at /users/${user}/matches (without the extra hash ID layer), just replace push() with set() instead. Here’s your updated code:
export const matchesInitialCreate = (user, matches) => { return (dispatch) => { firebaseApp.database().ref(`/users/${user}/matches`) .set(matches) // Replace push() with set() .then(() => { dispatch({ type: MATCHES_INITIAL_CREATE }); }); }; };
Quick Note on Use Cases
- Stick with
push()if you plan to add multiple separate match entries for a user (each entry will get its own unique ID to keep them organized). - Use
set()if thismatchesobject is the only one for the user—it will create or overwrite the/users/${user}/matchesnode directly with yourgroupsandknockoutdata.
内容的提问来源于stack exchange,提问作者Fernando Maymone
相关产品推荐
相关产品推荐

