Javascript 如何从计划功能访问Firestore中的文档ID
如果用户的详细信息添加到SendGrid联系人列表中,我将使用Firebase scheduled函数定期检查Firestore中的数据。成功将它们添加到SendGrid后,我想将firestoreJavascript 如何从计划功能访问Firestore中的文档ID,javascript,node.js,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,如果用户的详细信息添加到SendGrid联系人列表中,我将使用Firebase scheduled函数定期检查Firestore中的数据。成功将它们添加到SendGrid后,我想将firestoreaddToContact中的值从false更新为true exports.addContact = functions.region(region).pubsub.schedule('every 4 minutes').onRun(async(context) => { // Query
addToContact
中的值从false更新为true
exports.addContact = functions.region(region).pubsub.schedule('every 4 minutes').onRun(async(context) => {
// Query data from firestore
const querySnapshot = await admin.firestore().collection('users')
.where('metadata.addToContact', '==', false)
.get();
// Call SendGrid API to add contact
// If success, change metadata.addToContact to true
if (response) {
const docRef = admin.firestore().collection('users').doc(""+context.params.id);
await docRef.update( {'metadata.sendToSg': true }, { merge: true } );
}
}
我想访问context.params.id
,但我意识到传入.onRun的上下文与传入可调用函数的上下文不同
注意:如果我将context.params.id
传递到.doc
而不传递“+
,则参数“documentPath”不是有效的资源路径时会出现错误值。路径必须是非空字符串。在谷歌搜索答案后,我尝试使用“”+context.params.id
,然后我可以console.log上下文值。我只有
context {
eventId: '<eventID>',
timestamp: '2020-11-21T09:05:38.861Z',
eventType: 'google.pubsub.topic.publish',
params: {}
}
上下文{
事件ID:“”,
时间戳:“2020-11-21T09:05:38.861Z”,
eventType:'google.pubsub.topic.publish',
参数:{}
}
我想我应该从这里的params对象获取文档ID,但它是空的
是否有办法在计划函数中从firestore获取文档ID?本身没有firestore文档ID的概念,因为它是由而不是firestore文档上发生的事件触发的。这就是为什么对应的context
对象与Firestore触发的云函数的context
对象不同的原因
我知道您想更新与第一个查询相对应的唯一文档(admin.firestore().collection('users')。其中('metadata.addToContact','=','false')。get();
)
如果此理解正确,请执行以下操作:
exports.addContact = functions.region(region).pubsub.schedule('every 4 minutes').onRun(async (context) => {
// Query data from firestore
const querySnapshot = await admin.firestore().collection('users')
.where('metadata.addToContact', '==', false)
.get();
// Call SendGrid API to add contact
// If success, change metadata.addToContact to true
if (response) {
const docRef = querySnapshot.docs[0].ref;
await docRef.update({ 'metadata.sendToSg': true }, { merge: true });
} else {
console.log('No response');
}
return null;
});
exports.addContact = functions.region(region).pubsub.schedule('every 4 minutes').onRun(async (context) => {
// Query data from firestore
const querySnapshot = await admin.firestore().collection('users')
.where('metadata.addToContact', '==', false)
.get();
// Call SendGrid API to add contact
// If success, change metadata.addToContact to true
if (response) {
const promises = [];
querySnapshot.forEach((doc) => {
const { metadata } = doc.data();
if (metadata.sendToSg == false) {
promises.push(doc.ref.update({ 'metadata.sendToSg': true }, { merge: true }));
}
})
await Promise.all(promises);
} else {
console.log('No response');
}
return null;
});
根据您的评论更新: 您不应将
async/await
与forEach()
一起使用,有关更多详细信息,请参阅。改为使用,如下所示:
exports.addContact = functions.region(region).pubsub.schedule('every 4 minutes').onRun(async (context) => {
// Query data from firestore
const querySnapshot = await admin.firestore().collection('users')
.where('metadata.addToContact', '==', false)
.get();
// Call SendGrid API to add contact
// If success, change metadata.addToContact to true
if (response) {
const docRef = querySnapshot.docs[0].ref;
await docRef.update({ 'metadata.sendToSg': true }, { merge: true });
} else {
console.log('No response');
}
return null;
});
exports.addContact = functions.region(region).pubsub.schedule('every 4 minutes').onRun(async (context) => {
// Query data from firestore
const querySnapshot = await admin.firestore().collection('users')
.where('metadata.addToContact', '==', false)
.get();
// Call SendGrid API to add contact
// If success, change metadata.addToContact to true
if (response) {
const promises = [];
querySnapshot.forEach((doc) => {
const { metadata } = doc.data();
if (metadata.sendToSg == false) {
promises.push(doc.ref.update({ 'metadata.sendToSg': true }, { merge: true }));
}
})
await Promise.all(promises);
} else {
console.log('No response');
}
return null;
});
谢谢你的回复。我想说这是可行的,因为这给了我一个循环浏览文档的想法,因为我需要更新很多文档。我可以通过这种方式访问doc.id。(
if(response){querySnapshot.forEach(async(doc)=>{const{metadata}=doc.data();if(metadata.sendToSg==false){const docRef=admin.firestore().collection('users').doc(doc.id));等待docRef.update({'metadata.sendToSg:'true},{merge:true});})}
)请参阅更新。顺便说一句,如果我的答案给了你问题的解决方案,请接受它(参见)。谢谢