admin管理员组

文章数量:1315990

I am using this document provided by Firebase, to use a bination of Firebase and Firestore to establish "presence" in Firestore by using Firebase's built-in presence with onDisconnect(). Most of the code is verbatim from what Firebase provides, but I have customized a few things for my data structure. I have successfully deployed my function (below) to Firebase. Checking the logs, I get this error:

Error: Value for argument "seconds" is not a valid integer.
    at Object.validateInteger (/srv/node_modules/@google-cloud/firestore/build/src/validate.js:191:19)
    at new Timestamp (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:70:20)
    at Function.fromMillis (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:126:16)
    at Function.fromDate (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:108:26)
    at Serializer.encodeValue (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:97:53)
    at Serializer.encodeFields (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:56:30)
    at Function.fromObject (/srv/node_modules/@google-cloud/firestore/build/src/document.js:97:53)
    at op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:245:58)
    at writes._ops.map.op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:431:44)
    at Array.map (<anonymous>)

Since I am mostly using Firebase's provided code, I don't really know how to debug this. I am guessing it has something to do with the timestamp parisons between Firebase and Firestore. Any advice on where this could be going wrong?

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

const firestore=admin.firestore();

exports.onUserStatusChanged=functions.database.ref('user/{uid}').onUpdate(
    async (change, context) => {
        const eventStatus=change.after.val();
        const userStatusFirestoreRef=firestore.doc(`user/${context.params.uid}`);
        const statusSnapshot=await change.after.ref.once('value');
        const status=statusSnapshot.val();
        console.log(status, eventStatus);

        if (status.last_changed>eventStatus.last_changed){
            return null;
        }

        eventStatus.last_changed=new Date(eventStatus.last_changed);

        return userStatusFirestoreRef.set(eventStatus);
    }
);

I am using this document provided by Firebase, to use a bination of Firebase and Firestore to establish "presence" in Firestore by using Firebase's built-in presence with onDisconnect(). Most of the code is verbatim from what Firebase provides, but I have customized a few things for my data structure. I have successfully deployed my function (below) to Firebase. Checking the logs, I get this error:

Error: Value for argument "seconds" is not a valid integer.
    at Object.validateInteger (/srv/node_modules/@google-cloud/firestore/build/src/validate.js:191:19)
    at new Timestamp (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:70:20)
    at Function.fromMillis (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:126:16)
    at Function.fromDate (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:108:26)
    at Serializer.encodeValue (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:97:53)
    at Serializer.encodeFields (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:56:30)
    at Function.fromObject (/srv/node_modules/@google-cloud/firestore/build/src/document.js:97:53)
    at op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:245:58)
    at writes._ops.map.op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:431:44)
    at Array.map (<anonymous>)

Since I am mostly using Firebase's provided code, I don't really know how to debug this. I am guessing it has something to do with the timestamp parisons between Firebase and Firestore. Any advice on where this could be going wrong?

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

const firestore=admin.firestore();

exports.onUserStatusChanged=functions.database.ref('user/{uid}').onUpdate(
    async (change, context) => {
        const eventStatus=change.after.val();
        const userStatusFirestoreRef=firestore.doc(`user/${context.params.uid}`);
        const statusSnapshot=await change.after.ref.once('value');
        const status=statusSnapshot.val();
        console.log(status, eventStatus);

        if (status.last_changed>eventStatus.last_changed){
            return null;
        }

        eventStatus.last_changed=new Date(eventStatus.last_changed);

        return userStatusFirestoreRef.set(eventStatus);
    }
);
Share Improve this question asked Apr 16, 2020 at 18:50 BB DesignBB Design 70514 silver badges28 bronze badges 3
  • Here is a clue. I tried adding console.log statements to get the values of status.last_changed and eventStatus.last_changed, but they are both "undefined". So that sort of makes sense. So why does Firebase's own code include references to something that is undefined? – BB Design Commented Apr 16, 2020 at 20:49
  • It looks like that has to be pre-defined on those objects, prior to this script runnning. – BB Design Commented Apr 16, 2020 at 20:51
  • I added values for timestamps to both locations, now the error has gone away. – BB Design Commented Apr 16, 2020 at 21:04
Add a ment  | 

2 Answers 2

Reset to default 8

This error occurs as well, if you try to persist an invalid date such as:

new Date("string that cannot be parsed as timestamp/date")

The solution is that the last_changed elements needed to be pre-defined in the objects they are being referenced in. They were missing in this case, causing the error.

本文标签: