首页 > 解决方案 > Firebase 实时数据库 JS 错误:Reference.set 失败

问题描述

我在云功能中收到此错误

Error: Reference.set failed: First argument contains NaN in property 'server.saving-data.fireblog.history.newid.Id'
    at validateFirebaseData (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:1497:15)
    at /workspace/node_modules/@firebase/database/dist/index.node.cjs.js:1534:13
    at each (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:554:13)
    at validateFirebaseData (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:1517:9)
    at /workspace/node_modules/@firebase/database/dist/index.node.cjs.js:1534:13
    at each (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:554:13)
    at validateFirebaseData (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:1517:9)
    at validateFirebaseDataArg (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:1475:5)
    at Reference.set (/workspace/node_modules/@firebase/database/dist/index.node.cjs.js:13928:9)
    at /workspace/index.js:44:20 

我输入的js代码如下

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var db = admin.database();
var ref = db.ref("server/saving-data/fireblog");


exports.addHistory = functions.database.ref('issue/{id}').onCreate(async (snapshot, context) => {
  const val = snapshot.val();
  var lastid;

  return admin.database().ref('history').orderByChild("Id").limitToLast(1).on("child_added", function (snapshot) {
      snapshot.forEach(function (data) {
          lastid = data.val().Id;
      
      });
      const timeElapsed = Date.now();
      const today = new Date(timeElapsed);
      var datetime=today.toUTCString();

      var newid = lastid + 1;
          var usersRef = ref.child("history");
          usersRef.set({
              newid: {
                  Lamp: val.Id,
                  Reported: datetime,
                  Completed:"",
                  Status:"Pending",
                  AssignedTo:"",
                  Id:newid
              }
          });
  });


});

这是我需要添加记录的地方所以我在当前情况下得到的最后一个 Id 是 2 并以 1 为增量增加 3 并使用键 3 添加一条新记录

在此处输入图像描述

规则已定

在此处输入图像描述

标签: javascriptnode.jsfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


由于您正在读取history,并且已经使用单个子节点调用child_addedsnapshot回调中的 。这意味着不需要snapshot.forEach().

我还建议使用once而不是在on这里,因为您不想在读取一次值后保持侦听器处于活动状态。

最后,您还需要返回 promise fromusersRef.set()以确保 Cloud Function 在写入数据库完成之前不会中止。

全部结合起来,看起来像这样:

  return admin.database().ref('history').orderByChild("Id").limitToLast(1).once("child_added")
  .then(snapshot => {
      lastid = snapshot.val().Id;

      const timeElapsed = Date.now();
      const today = new Date(timeElapsed);
      var datetime=today.toUTCString();

      var newid = lastid + 1;
      var usersRef = ref.child("history");
      return usersRef.set({
          newid: {
              Lamp: val.Id,
              Reported: datetime,
              Completed:"",
              Status:"Pending",
              AssignedTo:"",
              Id:newid
          }
      });
  });

推荐阅读