我有一个功能,可以在关注另一个用户后更新用户的提要,方法是获取该用户帖子的帖子 ID,然后将其添加到用户文档中的“用户提要”中。在我添加时间戳字段之前,它工作得很好。我想做的是将每个帖子的时间戳添加到用户源中创建的文档中,以便我可以通过订购来获取源帖子按时间戳。
我遇到的错误:
错误:参数“data”的值不是有效的 Firestore 文档。无法使用“未定义”作为 Firestore 值(在“时间戳”字段中找到)。如果您想忽略未定义的值,请启用
。ignoreUndefinedProperties
exports.updateUserFeedAfterFollow = functions.firestore.document('/following/{currentUid}/user-following/{uid}').onCreate((snap, context) => {
const currentUid = context.params.currentUid;
const uid = context.params.uid;
const db = admin.firestore();
functions.logger.log('Function called..')
return db.collection('posts').where('ownerUid', '==', uid).get().then((snapshot) => {
functions.logger.log('Fetched posts..')
snapshot.forEach((doc) => {
const postId = doc.id;
const timestamp = doc.timestamp;
functions.logger.log('PostID:', postId)
const writeResult = db.collection('users').doc(currentUid).collection('user-feed').doc(postId);
writeResult.set({timestamp: timestamp});
});
return null;
})
.catch((err) => {
functions.logger.log(err);
});
});
我尝试将每个帖子的时间戳添加到创建的文档中。它不运行并给出错误
错误消息来自此行:
writeResult.set({timestamp: timestamp});
它表示
timestamp
的值为 undefined
,这在 Firestore 中无效。由于我们看不到查询结果或 timestamp
的值,因此我们无法告诉您更多有帮助的信息。您应该在写入之前检查该时间戳值以确保其有效。
此外,您还应该确保在从函数返回之前等待
set
返回的承诺得到解析。否则,Cloud Functions 可能会在代码完成之前终止您的代码。