Firebase - set priority when doing an update/fanout to several nodes

孤人 提交于 2020-01-04 01:32:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!