【发布时间】:2018-01-02 19:18:13
【问题描述】:
首先我必须说,我在 Java 方面有中级经验,并且在 JS 方面非常非常基础。
我正在尝试从我的数据库中删除过期的令牌,以实现我所做的:
function sendNotificationToUser(payload, userSnapshot) {
const userId = userSnapshot.id;
const user = userSnapshot.data();
let tokenMap = user.tokens;
const tokens = Object.keys(tokenMap);
const options = {priority: "high"};
admin.messaging().sendToDevice(tokens, payload, options).then(response => {
// For each message check if there was an error.
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
tokenMap.delete(tokens[index]);
}
} else{
console.log("Sent to user: ", user.name, " " ,user.surname, " notification: ", payload, " tokens: ", tokens[index]);
}
});
usersRef.doc(userId).update({
tokens: tokenMap
});
});
}
获取 tokenMap 的键没有问题,但看起来我无法使用 .delete() 删除条目,因为我在日志中找到了:
TypeError: tokenMap.delete 不是函数 在 response.results.forEach (/user_code/index.js:127:36) 在 Array.forEach (本机) 在 admin.messaging.sendToDevice.then.response (/user_code/index.js:122:26) 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)
是什么原因?
【问题讨论】:
-
你能显示试图调用 .remove 的代码吗?如果您尝试从
results中删除,则无论如何都不会将其从数据库中删除。它只会将其从您的结果集中删除。 -
对不起,调用的是tokenMap.delete(),我写错了:D
标签: node.js hashmap google-cloud-functions google-cloud-firestore