草庐IT

javascript - Cloud Functions - Cloud Firestore 错误 : can't get serverTimestamp

coder 2025-03-12 原文

Cloud Functions - Cloud Firestore 错误:无法获取服务器时间戳

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: Cannot read property 'serverTimestamp' of undefined

TypeError: Cannot read property 'TIMESTAMP' of undefined

最佳答案

正确的语法是:

firebase.firestore.FieldValue.serverTimestamp()

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

关于javascript - Cloud Functions - Cloud Firestore 错误 : can't get serverTimestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50361962/

有关javascript - Cloud Functions - Cloud Firestore 错误 : can't get serverTimestamp的更多相关文章

随机推荐