gpt4 book ai didi

javascript - 未处理的PromiseRejection警告: Unhandled promise rejection (rejection id: 1): ReferenceError: registrationTokens is not defined

转载 作者:行者123 更新时间:2023-12-02 23:51:44 27 4
gpt4 key购买 nike

当我尝试使用 Firestore 获取数据时,出现上述错误 - 我正在尝试从数据库检索 token ,以便可以发送消息:

exports.getUsers = functions.https.onRequest(async (req, res) => {
db.collection("users")
.get()
.then(snapshot => {
let registrationTokens = [];
snapshot.forEach(doc => {
let id = doc.id;

registrationTokens.push(id);
});
});

console.log(registrationTokens);

const message = {
data: { title: "Testing", body: "Test" },
tokens: registrationTokens
};

messaging.sendMulticast(message).then(response => {
console.log(response.successCount + " messages were sent successfully");
});
});

最佳答案

您已在 Promise 内声明了 RegistrationTokens,因此它在 Promise 外部不可见:

exports.getUsers = functions.https.onRequest(async (req, res) => {

db.collection("users")
.get()
.then(snapshot => {
let registrationTokens = [];
snapshot.forEach(doc => {
let id = doc.id;

registrationTokens.push(id);
});
console.log(registrationTokens);

// process the tokens
const message = {
data: { title: "Testing", body: "Test" },
tokens: registrationTokens
};

messaging.sendMulticast(message).then(response => {
console.log(response.successCount + " messages were sent successfully");
});


});
});

您需要记住,promise 是异步执行的,因此您需要等待结果出现。

关于javascript - 未处理的PromiseRejection警告: Unhandled promise rejection (rejection id: 1): ReferenceError: registrationTokens is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55611807/

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