云功能 - Cloud Firestore错误:无法获取serverTimestamp

时间:2018-05-16 03:14:07

标签: javascript google-cloud-firestore google-cloud-functions

云功能 - Cloud Firestore错误:无法获取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);
        });
    });

我分别犯了两个错误:

  

TypeError:无法读取未定义的属性“serverTimestamp”

     

TypeError:无法读取未定义

的属性'TIMESTAMP'

2 个答案:

答案 0 :(得分:5)

正确的语法是:

firebase.firestore.FieldValue.serverTimestamp()

注意()之后缺少括号(firestore):这是一个静态变量,而不是实例变量/成员字段。

答案 1 :(得分:0)

// Imports: Dependencies
const admin = require('firebase-admin');

// Correct Syntax (If You Don't Want To Import Firestore)
created_at: admin.firestore.FieldValue.serverTimestamp(),