【发布时间】:2020-05-10 14:32:15
【问题描述】:
我正在尝试从 firestore 数据库中查询特定文档。问题似乎是,如果我静态添加doc(id),它可以工作,但对于变量,它甚至没有正确且与我静态测试的值完全相同。
我试图检索的文档是User 节点/文档在/users 集合下。
read 是我用来检索数据的函数:
export default class GenericDB {
constructor(collectionPath) {
this.collectionPath = collectionPath
}
/**
* Read a document in the collection
* @param id
*/
async read(id) {
const result = await (await firestore())
.collection(this.collectionPath)
.doc(id)
.get()
const data = result.exists ? result.data() : null
if (isNil(data)) return null
this.convertObjectTimestampPropertiesToDate(data)
return { id, ...data }
}
}
这是我的 vuex 操作:
getUser: ({ commit }, userId) => {
return new Promise((resolve, reject) => {
//usin UsersDB() instead of Generic() because my UsersDB() has constructor with correct path to /users
new UsersDB().read(userId).then(user => {
//Empty user if userId value is from variable and not empty if I use static value
resolve(user)
})
})
}
我确实说出来了:
mounted() {
if (this.id) {
//getUser function is declared inside ...mapActions('authentication', ['getUser'])
this.getUser(this.id)
}
}
更新 1: 我确实使用逻辑运算符将静态字符串与我的变量进行了比较,结果发现变量 userId 末尾有空格。我不知道它为什么以及从哪里来。
没有错误只是空数据。我看不出这个简单的查询有什么问题。任何帮助表示赞赏!
【问题讨论】:
-
Firebase id 是自动生成的还是您正在定义?
-
@aryansingh 我使用自动生成的 UID 作为 id。
标签: javascript firebase vue.js google-cloud-firestore vuex