这是我的代码:
const deviceToken = admin.database().ref(`/users/{sender_user_id}/token_id`).once('value');
return deviceToken.then(result => {
const token_id = result.after.data();
const payload = {
notification: {
title: "New message!",
body: "You have a new message!",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
return console.log('This was a notification');
});
});
});
我收到此错误:
TypeError: Cannot read property 'data' of undefined at deviceToken.then.result (/srv/index.js:14:33)
我要从用户那里检索设备 token ID。我该怎么做?
节点--版本:v12.4.0 npm --版本:6.9.0
最佳答案
once
里面的方法 firebase.database.Reference
返回 Promise<DataSnapshot>
。类(class) admin.database.DataSnapshot
不包含名为 after
的属性。如果您尝试检索 token
从数据库中然后更改以下内容:
return deviceToken.then(result => {
const token_id = result.after.data();
进入此:
return deviceToken.then(result => {
const token_id = result.val();
来自文档:
Extracts a JavaScript value from a DataSnapshot.
Depending on the data in a DataSnapshot, the
val()
method may return a scalar type (string, number, or boolean), an array, or an object. It may also return null, indicating that theDataSnapshot
is empty (contains no data).
关于javascript - 我在 deviceToken.then.result (/srv/index.js :14:33)"at node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56697656/