[英]cloud functions and firestore save data into array
I try to get data from cloud firestore inside my actions on google app. 我尝试在Google应用程序中从Cloud Firestore获取数据。 I want to search for an object and then want to extract the link to the image in to an array.
我想搜索一个对象,然后将图像的链接提取到数组中。 If more objects were found the links should be pushed all in the array.
如果找到更多对象,则应将链接全部推入数组。
This is what I have: 这就是我所拥有的:
let array = []; //Everything should be in here
var collectionRef = db.collection('shoes');
var query = collectionRef.where('sport', '==', 'tennis');
query.get().then(function(results) {
if(results.empty) {
console.log("No documents found!");
} else {
// go through all results
results.forEach(function (doc) {
array.push(document.data().name);
});
}
}).catch(function(error) {
console.log("Error getting documents:", error);
});
var prodpic = array[0]; // Array is not filled because in output always "undefined"
I do want to work with the img-links in prodpic in my later program. 我确实想在以后的程序中使用prodpic中的img-links。 I hope my problem is clear and anyone can help me to find my mistake.
我希望我的问题很清楚,任何人都可以帮助我找到我的错误。
Data is loaded from Cloud Firestore asynchronously. 数据是从Cloud Firestore异步加载的。 This means that by the time you assign
var prodpic = array[0]
it hasn't been loaded yet. 这意味着,当您分配
var prodpic = array[0]
它尚未被加载。
This is easiest to see by replacing most of the code with a few log statements: 通过用一些日志语句替换大多数代码,最容易看到这一点:
console.log("Before starting query");
query.get().then(function(results) {
console.log("In query results");
})
console.log("After starting query");
When you run this code the output is: 运行此代码时,输出为:
Before starting query
开始查询之前
After starting query
开始查询后
In query results
在查询结果中
This probably isn't what you expected, but explains perfectly why your var prodpic = array[0]
doesn't work: the data hasn't been loaded yet, so array[0]
is empty. 这可能不是您所期望的,但是可以很好地解释为什么
var prodpic = array[0]
不起作用:尚未加载数据,所以array[0]
为空。
To deal with asynchronous loading, you have to make sure that your code that needs the data is inside the then()
callback. 要处理异步加载,必须确保需要数据的代码在
then()
回调内部 。 So: 所以:
query.get().then(function(results) {
if(results.empty) {
console.log("No documents found!");
} else {
// go through all results
results.forEach(function (doc) {
array.push(document.data().name);
});
var prodpic = array[0];
}
}).catch(function(error) {
console.log("Error getting documents:", error);
});
For more on this, also read Doug's blog post here: https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93 有关此的更多信息,请在以下位置阅读Doug的博客文章: https : //medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93
One additional optimization: if you only care about the first matching document, it's more efficient to use a limit
query: 另一个优化:如果您只关心第一个匹配的文档,则使用
limit
查询会更有效:
var query = collectionRef.where('sport', '==', 'tennis').limit(1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.