gpt4 book ai didi

javascript - Firebase 管理员 : How to promisify a push operation

转载 作者:行者123 更新时间:2023-11-30 09:23:59 25 4
gpt4 key购买 nike

我有一个函数如下。

const notify = function (uid, ..., senderId) {
var obj = {
"time": +new Date(),
... ...
"uid": senderId,
}
dbRef("../notifications/").push(obj);
}

我从其他函数中调用它来编写通知,这是在返回 promise 的其他函数中调用的,这些 promise 被放入一个数组中以等待所有 promise 的实现。

所以我希望它返回一个 promise ,而不是使用一个完整的回调,你知道,就像 db.set() 那样。

为什么我不使用 db.set()

结构。

users/{uid}/notifications/autoId/notificationObject.

db.ref('users/{uid}/notifications').push(notificationObject) 将遵循上述内容,而 set() 将执行以下操作.

`users/{uid}/notifications/notificationObject`

这实际上只会替换整个用户通知对象。

最佳答案

有多种方法可以像您问题中的那样编写 push() 函数:

dbRef("../notifications/").push(obj);

其中一个选项使用 set() 如下:

dbRef("../notifications/").push().set(obj);

这实际上是做以下事情:

//Generate the unique push key and get the reference
var key = dbRef("../notifications/").push();
//Save the data to the newly generated reference
key.set(obj);

此答案中的所有三个代码块都做完全相同的事情。有关这些保存方式的更多信息,请参阅 the Firebase docs .

关于javascript - Firebase 管理员 : How to promisify a push operation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49917380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com