问题
Cloud Functions - Cloud Firestore error: can't get serverTimestamp
const admin = require('firebase-admin');
exports.userlog = functions.firestore
.document('user/{userId}')
.onUpdate((change, context) =>
{
const db = admin.firestore();
//var timestamp = db.FieldValue.serverTimestamp();
var timestamp = db.ServerValue.TIMESTAMP;
...
return db.collection('userlog').add(
{
userId : previousValue.userId,
...
timestamp: timestamp
}).then(ref =>
{
return console.log('Added document with ID: ', ref.id);
});
});
I got two errors separately:
TypeError: Cannot read property 'serverTimestamp' of undefined
TypeError: Cannot read property 'TIMESTAMP' of undefined
回答1:
The correct syntax is:
firebase.firestore.FieldValue.serverTimestamp()
Note the lack of parenthesis (()) after firestore: this is a static variable, not an instance variable/member field.
来源:https://stackoverflow.com/questions/50361962/cloud-functions-cloud-firestore-error-cant-get-servertimestamp