Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
Firebase Firestore云函数-在.then()中无法识别外部变量的值_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Firebase Firestore云函数-在.then()中无法识别外部变量的值

Firebase Firestore云函数-在.then()中无法识别外部变量的值,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我不熟悉Firestore云函数,尝试运行一个简单的函数,在一个集合中触发onWrite(),并在此基础上更新另一个集合中的值 我遇到了一个障碍,变量(实际上是常量数组)的值在…get()中无法识别。然后(snapshot)=>{}方法。据我所知,外部变量的作用域也应该在内部,不是吗?我做错了什么 对这个主题的研究让我想到了承诺的概念,我尝试基于get()创建承诺值并返回它们,但也无法实现。也许我用错了,如果这是答案,请分享。多谢各位 更新: 我通过将数组的值存储在.then()函数外部的变量中

我不熟悉Firestore云函数,尝试运行一个简单的函数,在一个集合中触发onWrite(),并在此基础上更新另一个集合中的值

我遇到了一个障碍,变量(实际上是常量数组)的值在…get()中无法识别。然后(snapshot)=>{}方法。据我所知,外部变量的作用域也应该在内部,不是吗?我做错了什么

对这个主题的研究让我想到了承诺的概念,我尝试基于get()创建承诺值并返回它们,但也无法实现。也许我用错了,如果这是答案,请分享。多谢各位

更新: 我通过将数组的值存储在.then()函数外部的变量中进行了测试,然后重试,结果正常。 似乎正在发生的问题是const myArray[]

我的代码如下:

exports.mytestExport = functions.firestore
    .document('col/{docid}')
    .onWrite( (change, context) => {

        const docID = context.params.docid;
        const myArray = change.after.data().someArray;

        const db = admin.firestore();
        const newColRef = db.collection("newCollection");

        for(index = 0; index < myArray.length; index++) {
            console.log(myArray[index]);      //Value Prints here as expected.
            var myArrayValue = myArray[index];
            console.log(typeof(myArray[index])); // Output is 'string'

            let snapshot = newColRef.doc(myArray[index]).get()
                        .then((snapshot) => {

                            console.log(myArray[index]);  // This prints **'undefined'** for all the times 'for' loop is executed.
                            console.log(myArrayValue); // This prints Correctly!

                        if(snapshot.exists) {
                                //my code to update 'newCollection'
                            }
                        })
        }
        return 'success';
    })
exports.mytestExport=functions.firestore
.document('col/{docid}'))
.onWrite((更改、上下文)=>{
const docID=context.params.docID;
const myArray=change.after.data().someArray;
const db=admin.firestore();
const newColRef=db.collection(“newCollection”);
对于(索引=0;索引{
console.log(myArray[index]);//在执行“for”循环的所有时间内打印**'undefined'**。
console.log(myArrayValue);//打印正确!
if(snapshot.exists){
//更新“newCollection”的我的代码
}
})
}
回归"成功",;
})

非常感谢你的帮助!谢谢,

正如@Doug Stevenson所回应的,造成这种情况的原因确实是没有正确地使用承诺

对于将来有类似问题的任何人,请在下面发布我的认识:

因为在进入下一步for循环之前,代码不应该等待任何承诺

for(index = 0; index < myArray.length; index++) {

一旦我对链接(.then..then..catch..response())更熟悉,我会更喜欢使用它。

这里肯定没有正确使用承诺。这是在云函数上使用JavaScript进行异步编程的一个关键概念。如果不了解承诺是如何工作的,就不可能有效地编写函数。这里的函数需要返回一个承诺,该承诺只有在所有异步工作完成后才能解析。现在,它马上就完成了,而且所有的工作都不可能完成。如果您查看这些关于在云功能中使用承诺的视频,可能会有所帮助。非常感谢。我正在看《承诺》系列视频。但是,由于外部变量的打印并不取决于内部函数是否返回值(或完全执行),因此我不太相信是承诺导致了错误。任何其他console.log()消息都可以正常运行并打印值,如果这个变量是从outerscope传递的,那么它也应该可以打印/工作!你当时一定是对的:D
let snapshot = await newColRef.doc(myArray[index]).get()
if (snapshot.exists)....{}