我正在尝试从 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
末尾有空格。我不知道为什么以及它来自哪里。
没有错误只是空数据。我看不出这个简单的查询有什么问题。任何帮助表示赞赏!
事实证明,如果我将它们与逻辑运算符进行比较,则 userId 并不完全相同。变量版本末尾有空格。
所以解决方案是使用 userId.replace(/\s/g, '')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句