javascript - 我在 node.js 处收到这种错误“TypeError:无法读取 deviceToken.then.result (/srv/index.js:14:33) 处未定义的属性‘数据’”
问题描述
这是我的代码:
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。我该怎么做?
节点 --version: v12.4.0 npm --version: 6.9.0
解决方案
里面的once
方法firebase.database.Reference
返回一个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
为空(不包含数据)。
推荐阅读
- c++ - 如何在.cpp中实现属于非模板类的模板函数
- python - Python - 使用具有多个参数的多处理不起作用
- angular - 订阅 observable 返回 undefined
- apache-spark - Spark & PySpark:表级访问控制?
- filter - 有没有办法使用 solr 文本标记器和 n-edge-gram 过滤器
- javascript - 如何修复正在声明但从未读取其值的 Javascript 滑块按钮功能?
- google-json-api - 如何从 Google JSON API 获得准确的结果,就像真正的 Google 搜索一样
- git - Composer 需要自定义包缓存问题
- unit-testing - 使用 `vue-test-utils` 和 `jest` 使用 `Updated` 钩子进行测试
- node.js - 使用 NodeJS 和 Cheerio 抓取需要太多时间