Firebase Realtime Database JS Error : Reference.set failed

Viewed 173

I get this error in my cloud function

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 

the js code that i have typed is as follows

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
              }
          });
  });


});

This is where i need to add record So i get the last Id in the current case is 2 and increment by 1 making 3 and add a new record with key 3

enter image description here

Rules have been set

enter image description here

1 Answers

Since you're reading from history, and listening to child_added the snapshot in your callback gets called with an individual child node already. This means there's no need for snapshot.forEach().

I'd also recommend using once instead of on here, as you don't want to keep the listener active after reading the value once.

Finally, you also need to return the promise from usersRef.set() to ensure the Cloud Function doesn't get aborted before the write to the database is completed.

All combined, that looks like this:

  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
          }
      });
  });
Related