问题描述
我需要用一个生成的键创建一个新的对象并更新其他一些位置,它应该是原子的。有没有办法用多位置更新进行推送,还是必须使用旧的交易方式?这适用于任何客户端平台,但这里有一个JavaScript例子。
有两种方法可以调用在Firebase的JavaScript SDK中推送。使用 push(newObject),
$ b
。这将生成一个新的推式ID,并将数据写入到具有该ID的位置。使用 push()$ c
$ C>。这将生成一个新的推式ID,并返回一个引用到该ID的位置。这是一个纯粹的客户端操作。
了解#2,您可以轻松得到一个新的推客户端客户端:
var newKey = ref.push()。key();
然后,您可以在多位置更新中使用此键。
I need to create a new object with a generated key and update some other locations, and it should be atomic. Is there some way to do a push with a multi-location update, or do I have to use the old transaction method? This applies for any client platform, but here's an example in JavaScript.
var newData = {}; newData['/users/' + uid + '/last_update'] = Firebase.ServerValue.TIMESTAMP; newData['/notes/' + /* NEW KEY ??? */] = { user: uid, ... }; ref.update(newData);解决方案There are two ways to invoke push in Firebase's JavaScript SDK.
using push(newObject). This will generate a new push id and write the data at the location with that id.
using push(). This will generate a new push id and return a reference to the location with that id. This is a pure client-side operation.
Knowing #2, you can easily get a new push id client-side with:
var newKey = ref.push().key();You can then use this key in your multi-location update.
这篇关于Firebase:我可以将推送与多位置更新结合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!