【问题标题】:Return documents ID in snapshot firestore JS在快照 Firestore JS 中返回文档 ID
【发布时间】:2020-11-10 19:55:52
【问题描述】:

我是 Coding 和 Firestore 的新手,由于某种原因,我无法正确理解。 我想返回 doc.id 但我得到的输出是 undefined 它似乎没有返回任何数据

console.log(doc.id) 的输出是hqyWHGrVHvopdyVACRkn ID 的输出是 undefined

const getProductDocId = async (productName1) =>{
  try{
    const ID = await firestore().collection('product').where('productName' ,'==', productName1).get().then(snapshot=>{
      snapshot.docs.every(doc => {
        console.log(doc.id + " THIS is DOC.ID")
        return doc.id
      })
    })
    
    console.log(ID)
 
   }catch(error){
     console.log("Error @ Get Prdocut ID  " + error)
   }finally{
     console.log(ID)
   }
 } 

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您以错误的方式申请await。如果将Promise 传递给await 表达式,它会等待Promise 被执行并返回已执行的值。

    try {
      const snapshot = await firestore()
        .collection("product")
        .where("productName", "==", productName1)
        .get();
      let id = null;
      snapshot.docs.every((doc) => {
        console.log(doc.id + " THIS is DOC.ID");
        id = doc.id;
      });
      console.log(id);
    } catch (error) {
      console.log("Error @ Get Prdocut ID  " + error);
    } finally {
      console.log(id);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 2021-04-07
      • 2020-12-16
      • 2019-08-02
      • 1970-01-01
      • 2020-10-12
      • 2020-11-07
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多