问题
I'm writing to several parts/nodes of my Firebase database in one update. Is it possible to set the priority of a node when doing this? Example:
firebaseRef.update([
"/some/node/": "value",
"/some/other/node": "other value"
])
What if I want to set the priority of the node at "/some/other/node" at the same time. Is that possible? Something like:
firebaseRef.update([
"/some/node/": "value",
"/some/other/node": {
value: "other value",
priority: 0 - Date.now()
}
])
回答1:
You almost got it, the "magic" property is named .priority:
firebaseRef.update([
"/some/node/": "value",
"/some/other/node": {
value: "other value",
".priority": 0 - Date.now()
}
]
Note that there aren't really a lot of reasons to use priorities anymore in Firebase. Adding a normally named property (e.g. "reversedTimestamp": 0 - Date.now()) will accomplish the same and is less hidden/magic.
Update
It seems you want to update the node's value, not a child named value. To set the (primitive) value and priority in one go, use:
firebaseRef.update([
"/some/node/": "value",
"/some/other/node": {
".value": "other value",
".priority": 0 - Date.now()
}
]);
回答2:
I recommend using:
".priority": Firebase.ServerValue.TIMESTAMP
It creates a timestamp from the server. It makes sure that our data is in the order it was created in, even if our clients are in different time zones.
来源:https://stackoverflow.com/questions/35042529/firebase-set-priority-when-doing-an-update-fanout-to-several-nodes