簡體   English   中英

我在node.js遇到了這樣的錯誤“ TypeError:無法讀取deviceToken.then.result(/srv/index.js:14:33)中未定義的屬性'data'”

[英]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();

從文檔:

val

從DataSnapshot中提取JavaScript值。

根據DataSnapshot中的數據, val()方法可能返回標量類型(字符串,數字或布爾值),數組或對象。 它還可能返回null,指示DataSnapshot為空(不包含數據)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM