![](/img/trans.png)
[英]index.js:33 Uncaught TypeError: Cannot read property 'question' of undefined at loadQuiz (index.js:33)
[英]I am getting this kind of error “TypeError: Cannot read property 'data' of undefined at deviceToken.then.result (/srv/index.js:14:33)” at node.js
這是我的代碼:
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:無法讀取deviceToken.then.result(/srv/index.js:14:33)中未定義的屬性“數據”
我要從用戶檢索設備令牌ID。 我該怎么做?
節點-版本:v12.4.0 npm-版本:6.9.0
firebase.database.Reference
內部的once
方法返回Promise<DataSnapshot>
。 admin.database.DataSnapshot
類不包含after
的屬性。 如果您嘗試從數據庫中檢索token
,請更改以下內容:
return deviceToken.then(result => {
const token_id = result.after.data();
到這個:
return deviceToken.then(result => {
const token_id = result.val();
從文檔:
從DataSnapshot中提取JavaScript值。
根據DataSnapshot中的數據,
val()
方法可能返回標量類型(字符串,數字或布爾值),數組或對象。 它還可能返回null,指示DataSnapshot
為空(不包含數據)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.