【发布时间】:2020-04-12 05:21:38
【问题描述】:
这是我的数据库的结构: challenges table & users table
这是我得到的错误:error image
我想使用“created_by”字段,它也是 users 表的文档 ID,我想在其中检索显示名称和照片 URL。
我并不完全确定 Promise 是如何工作的,我有一种感觉,这就是我在挣扎的原因,但到目前为止我的代码如下:
数据检索:
UsersDao.getUserData(ChallengesDao.getChallenges().then(result => {return result['author'][0]})).then(result => {console.log(result)})
挑战 DAO:
export default class ChallengesDao {
static async getChallenges() {
const db = require('firebase').firestore();
// const challenges = db.collection('challenges').limit(number_of_challenges)
// challenges.get().then(())
const snapshot = await db.collection('challenges').get()
const names = snapshot.docs.map(doc => doc.data().name)
const createdBy = snapshot.docs.map(doc => doc.data().created_by)
const highScores = snapshot.docs.map(doc => doc.data().high_score.score)
return {challengeName: names, author: createdBy, score: highScores}
}
用户 DAO:
const db = require('firebase').firestore();
export default class UsersDao {
static async getUserData(uid: string) {
let userData = {};
try {
const doc = await db
.collection('users')
.doc(uid)
.get();
if (doc.exists) {
userData = doc.data();
} else {
console.log('User document not found!');
}
} catch (err) {}
return userData;
}
}
【问题讨论】:
-
如果有任何错误或某些错误,必须显示您的输出,因为我们很容易理解。谢谢
标签: javascript typescript firebase web google-cloud-firestore