Firebase 使用云函数更新不同的集合文档时出错
通过使用云功能,当编辑“用户”集合中的文档时,无论用户id存储在何处,编辑的文件都应在Firebase 使用云函数更新不同的集合文档时出错,firebase,google-cloud-functions,Firebase,Google Cloud Functions,通过使用云功能,当编辑“用户”集合中的文档时,无论用户id存储在何处,编辑的文件都应在uploadscollection中更新 对于上述要求,我使用以下功能 const functions = require('firebase-functions'); const admin = require('firebase-admin'); const settings = { timestampsInSnapshots: true }; admin.initializeApp();
uploads
collection中更新
对于上述要求,我使用以下功能
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const settings = {
timestampsInSnapshots: true
};
admin.initializeApp();
admin.firestore().settings(settings);
var db = admin.firestore();
exports.updateUser = functions.firestore.document('users/{userId}')
.onUpdate((change, context) => {
var userId = context.params.userId;
const newValue = change.after.data();
const name = newValue.display_name;
var uploadsRef = db.collection('uploads');
uploadsRef.where('user.id', '==', userId).get().then((snapshot) => {
snapshot.docs.forEach(doc => {
doc.set({"display_name" : name}); //Set the new data
});
}).then((err)=> {
console.log(err)
});
});
当执行此操作时,我在日志中得到以下错误
TypeError: doc.set is not a function
at snapshot.docs.forEach.doc (/user_code/index.js:31:21)
at Array.forEach (native)
at uploadsRef.where.get.then (/user_code/index.js:29:27)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
还有下面的
Unhandled rejection
我如何处理这个问题?处理快照文档更新的最佳方法是什么?在对象上执行快照更新时,将产生快照更新
对象当您使用其docs属性时,您正在迭代包含匹配文档中所有数据的对象数组。看起来您假设QuerySnapshotDocument对象有一个set()方法,但从链接的API文档中可以看出它没有
如果要回写QuerySnapshotDocument中标识的文档,请使用其属性获取具有方法的对象
请记住,如果进行此更改,它将运行,但可能不会更新所有文档,因为您也忽略了set()方法返回的承诺。您需要将所有这些承诺收集到一个数组中,并使用Promise.all()生成从函数返回的新承诺。这对于帮助云功能了解所有异步工作何时完成是必要的。您在回答中有什么建议吗?您能告诉我upload collection的用途吗?是否用于存储更新的数据?
doc.ref.set({"display_name" : name}); //Set the new data