Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从计划功能访问Firestore中的文档ID_Javascript_Node.js_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 如何从计划功能访问Firestore中的文档ID

Javascript 如何从计划功能访问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

如果用户的详细信息添加到SendGrid联系人列表中,我将使用Firebase scheduled函数定期检查Firestore中的数据。成功将它们添加到SendGrid后,我想将firestore
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});})}
)请参阅更新。顺便说一句,如果我的答案给了你问题的解决方案,请接受它(参见)。谢谢