简体   繁体   English

Firebase function error: Cannot convert undefined or null to object at Function.keys (<anonymous> )</anonymous>

[英]Firebase function error: Cannot convert undefined or null to object at Function.keys (<anonymous>)

Description of the problem:问题描述:

My App aim is to store family spending in Firebase Realtime Database.我的应用程序的目标是将家庭支出存储在 Firebase 实时数据库中。 I want that, when a new spending is stored, a notification is sent to all other devices.我希望,当存储新支出时,会向所有其他设备发送通知。 I try to send a notification to a single device and it works fine, but when I try to get all the tokens in an array, I have an error: TypeError: Cannot convert undefined or null to object at Function.keys ().我尝试向单个设备发送通知并且它工作正常,但是当我尝试获取数组中的所有令牌时,出现错误:TypeError: Cannot convert undefined or null to object at Z86408593C34AF77FDD90DFs932F8。

code of index.js : index.js 的代码

const functions = require("firebase-functions");
const admin = require("firebase-admin");
 admin.initializeApp();

exports.androidPushNotification = functions.database
    .ref("{nodo}/spese/{spesaPush}")
    .onCreate(async (snapshot, context) => {
      const original = snapshot.val();

      const getDeviceTokensPromise = admin.database()
          .ref(`/utenti/{tutti}/token`).once('value');

      let tokensSnapshot;
      let tokens;
      tokensSnapshot = await getDeviceTokensPromise;

      // This is the line generating the errors. 
      // If I get only a specific token and 
      // use tokens = tokensSnapshot.val(); anything works fine
      tokens = Object.keys(tokensSnapshot.val());


      const result = original.chi + " ha speso " +
      original.costo + " € per acquistare " +
      original.desc;
      console.log(result);

      const payload = {
        notification: {
          title: 'New spending inserted!',
          body: result, 
        }
      };

      const response = await admin.messaging().sendToDevice(tokens, payload);
      return result;
    });

It seems that values are not reached yet, but I thought that the keyword await lets system to wait until all data are available.似乎尚未达到值,但我认为关键字 await 让系统等待所有数据可用。 From the log I noticed that the value I need is null and I don't understand why.从日志中我注意到我需要的值是 null 我不明白为什么。 If I use this line of code:如果我使用这行代码:

      const getDeviceTokensPromise = admin.database()
          .ref(`/utenti/SpecificUser/token`).once('value');
      ....
      //then
      tokens = tokensSnapshot.val();

The notification is sent to the device that has the token under the name of "SpecificUser"通知将发送到具有“SpecificUser”名称下令牌的设备

EDIT: I provide a pic of my db.编辑:我提供了我的数据库的图片。 I notice that none of the field is null, so I don't know why I see this error我注意到没有一个字段是 null,所以我不知道为什么我看到这个错误在此处输入图像描述

Thank you to anyone that helps me感谢任何帮助我的人

i had same error and it is solve by database... when i saw my database values unfortunately i stored undefined value so my whole result got error like you... see your whole values and fields that store values properly.我有同样的错误,它是由数据库解决的......当我看到我的数据库值不幸的是我存储了未定义的值,所以我的整个结果像你一样出现错误......查看你的整个值和正确存储值的字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法将 Firebase 时间戳转换为 ISO 格式,因为我收到此错误:TypeError: thread.createdAt.toDate is not a function - I cannot convert a Firebase Timestamp to ISO format because I get this error: TypeError: thread.createdAt.toDate is not a function 无法从源加载 function 定义:无法从 function 源生成清单:错误:找不到模块“firebase-admin/auth” - Failed to load function definition from source: Failed to generate manifest from function source: Error: Cannot find module 'firebase-admin/auth' Firebase 基础主机重写规则到 function -“无法获取”/“无法发布” - Firebase base hosting rewrite rules to function - "Cannot GET"/"Cannot POST" android firebase crashlytics 无法在 null ZA8CFDE6331BD59EB2AC966F8911Z4B6 上调用方法 all() - android firebase crashlytics Cannot invoke method all() on null object 本地模拟器 Firebase 云 Function 无法获取和 404 - Local Emulator Firebase Cloud Function Cannot Get and 404 Firebase Cloud Functions - 如何调用另一个函数并返回对象 - Firebase Cloud Functions - How to call another function and return object Firebase 云功能:部署 function 时出错 - Firebase Cloud Functions: Error while deploying function Firebase 云函数返回内部错误 - Firebase Cloud Function returns INTERNAL error Firebase Function 无法从源加载 function 定义错误解决方案 - Firebase Function Failed to load function definition from source error Solution Firebase 可调用云 Function CORS 错误使用 Z035489FF8D092741943E4A83241F5 - Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM