【问题标题】:Firestore specific data read does not work with variable but works with static valueFirestore 特定数据读取不适用于变量,但适用于静态值
【发布时间】: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


【解决方案1】:

尝试以这种方式建立连接,而不是直接使用它。

const db = firebase.firestore();
async function read(id) {
    const result = await db
        .collection(this.collectionPath)
        .doc(id)
        .get()

    const data = result.exists ? result.data() : null

    if (isNil(data)) return null

    this.convertObjectTimestampPropertiesToDate(data)
    return { id, ...data }
}

一般来说,我们主要用来获取文档的标准格式是:

const db = firebase.firestore();

const result = await db
    .collection("collection_name")
    .doc("document_id")
    .get();

我希望这对你有帮助。如有任何问题,请告诉我。

在您编辑问题后,我尝试传递一个有效变量并得到响应。这是给我文档数据。

//Firebase
const admin = require("firebase-admin");
let serviceAccount = require("./firebase.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});
let db = admin.firestore();
//End of Firebase

id = "lWxkvqZnBxNRke4SFyJj"
async function getData(id) {
    const result = await db
        .collection("users")
        .doc(id)
        .get();

    data = result.data()
    console.log(data)
    return data
}
getData(id)

【讨论】:

  • 抱歉,除了函数声明语法和初始化方式firestore()之外,您的答案似乎没有任何区别
  • 但是嵌套的 await 调用可能无法正确解析 promise。试试看吧
  • 我初始化 Firestore 的方式可以提高性能并且没有任何问题,因为正如我所说,它确实在查询中提供了静态值的值。
【解决方案2】:

如果我将它们与逻辑运算符进行比较,结果发现 userId 并不完全相同。变量版本末尾有空格。

所以解决方案是使用userId.replace(/\s/g, '')

【讨论】:

    【解决方案3】:

    我最近遇到了同样的问题。然后我发现我的字符串有引号。下面的代码解决了这个问题。

    roomId.trim().replace(/['"]+/g, '')
    

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多