Typescript Firestore将值获取到数组中,但它是未定义的
我目前正在使用前端框架Ionic和angularjs以及typescript开发一个移动应用程序,后端使用firebase。我的问题是,当我调用firebase nosql数据库时,我无法将我的项目放入typescript数组中,以便将数据发送到屏幕,因为当我调用包含值的变量时,它是未定义的。这是我的密码:Typescript Firestore将值获取到数组中,但它是未定义的,typescript,firebase,ionic-framework,google-cloud-firestore,Typescript,Firebase,Ionic Framework,Google Cloud Firestore,我目前正在使用前端框架Ionic和angularjs以及typescript开发一个移动应用程序,后端使用firebase。我的问题是,当我调用firebase nosql数据库时,我无法将我的项目放入typescript数组中,以便将数据发送到屏幕,因为当我调用包含值的变量时,它是未定义的。这是我的密码: getSingleDatabase(){ this.collectionReference=this.db.collection('posts'); this.collectio
getSingleDatabase(){
this.collectionReference=this.db.collection('posts');
this.collectionReference.get()
.then(snapshot =>{
snapshot.forEach(doc => {
this.firstGet.push(doc.data());
});
})
.catch(err =>{
console.log(err);
});
return this.firstGet;
}
Firstore API是异步的,这意味着您对get()的调用立即返回,并带有一个承诺,指示提取实际完成的时间。它可能需要任何时间,直到承诺与您正在寻找的文件实现。因此,getSingleDatabase函数将在
this.firstGet
中的数据可用之前返回。如果在代码的每个阶段都使用日志语句,您将能够理解我的意思
知道数据何时完全可用的唯一方法是使用get()返回的承诺,这意味着您不应该期望this.firstGet
包含任何内容,直到承诺实现为止
以及对他们的期望