Firebase-admin-node: [Firestore] Error: Cannot encode value: serverTimestamp()

Created on 23 Dec 2019  路  2Comments  路  Source: firebase/firebase-admin-node

[REQUIRED] Step 2: Describe your environment

  • Operating System version: NA
  • Firebase SDK version: "firebase-admin": "^8.2.0"
  • Firebase Product: Firestore
  • Node.js version: v10.12.0
  • NPM version: 6.9.0

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

I'm getting error for using admin.firestore.FieldValue.serverTimestamp inside transaction.create.

The same is working in a previous deployed different functions. But this is not working this functions deployed now.

Here is my sample code:

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

//...
exports.monthlyReports = functions.firestore.document('transactions/{transactionId}').onWrite((snap, context) => {

return firestore.runTransaction(transaction => {

    return transaction.get(reportsRef).then(doc => {

        //....

        transaction.create(eventIdRef, { s: admin.firestore.FieldValue.serverTimestamp } );

        //...

        return Promise.resolve();
    });

});

});

I'm getting the following error:

Error: Cannot encode value: serverTimestamp() {
        return ServerTimestampTransform.SERVER_TIMESTAMP_SENTINEL;
    }
    at Serializer.encodeValue (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:160:15)
    at Serializer.encodeFields (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:65:34)
    at Function.fromObject (/srv/node_modules/@google-cloud/firestore/build/src/document.js:93:53)
    at op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:147:58)
    at writes._ops.map.op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:429:44)
    at Array.map (<anonymous>)
    at WriteBatch.commit_ (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:429:34)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
firestore needs-triage

Most helpful comment

serverTimestamp is a function and should be invoked. Your code is passing it as a value. See https://googleapis.dev/nodejs/firestore/latest/FieldValue.html#.serverTimestamp

All 2 comments

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

serverTimestamp is a function and should be invoked. Your code is passing it as a value. See https://googleapis.dev/nodejs/firestore/latest/FieldValue.html#.serverTimestamp

Was this page helpful?
0 / 5 - 0 ratings