由于firestore数据库调用而从promise获取数据



我正试图从我的firestore数据库中获取数据,然后将返回的对象数组使用到我的模板中。我所期望的对象数组实际上是一个承诺。我似乎不知道如何提取它的数据。

这是一个函数,它对我的firestore数据库进行两次调用,然后将两个结果组合到一个数组中。func返回这个数组。

async getOtherArticles (src, target){
try {
var tempArticles = []
const q = query(collection(projectFirestore, "database")
,where('target', '==', target)
,where('src','==',src));
const docs = await getDocs(q);

if(docs){
docs.forEach((doc) => { tempArticles.push(doc.data()) })
}

const q2 = query(collection(projectFirestore, "database")
,where('target', '==', src)
,where('src','==',target));
const docs2 = await getDocs(q2);
if(docs2){
docs2.forEach((doc2) => { tempArticles.push(doc2.data()) })
}
return tempArticles

} catch (err) {
throw new Error(err.message) 
}
}

然后我简单地调用前面的函数,以获得它的结果,如下面的示例所示

this.similarArticles = this.getOtherArticles(this.drawerLinkData.src,this.drawerLinkData.target).then( function(val){return val})

当我控制台日志结果,我得到:

Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Array(1)
0: {region: Array(1), technology: Array(1), target: 'Yandex', video: '', summary: '<p>Yandex announced that it has entered into a bin…rship in the business.</strong></p>n<p>&nbsp;</p>', …}
length: 1
[[Prototype]]: Array(0)

我只想得到[[PromiseResult]]中的内容。我怎样才能做到这一点?

正如@Keith所建议的那样,添加wait就成功了。更正了以下代码:

this.similarArticles = await this.getOtherArticles(this.drawerLinkData.src,this.drawerLinkData.target)

最新更新