代码之家  ›  专栏  ›  技术社区  ›  Daniel

获取云函数中的文档值

  •  0
  • Daniel  · 技术社区  · 7 年前

    我的云Firestore数据库如下所示:

    users
     |          
     |----id----(name,age)
     |----id----(name,age)
     |----id----(name,age)
     |----id----(name,age)
     |----id----(name,age)
    ...
    

    我正在通过所有元素编写查询:

    db.collection('users').get()
    .then(
        (results) => {          
            results.forEach((doc) => {
                //how to get the id that represents this doc?
            });
            response.json();
        }           
    )
    .catch(function (error) {
        console.error("Error getting user: ", error);
        response.send(error)
    });
    

    医生 医生

    1 回复  |  直到 7 年前
        1
  •  1
  •   Frank van Puffelen    7 年前

    这其实很简单。这个 doc 变量实际上是 DocumentSnapshot ,您可以从它的 id 所有物因此:

    db.collection('users').get()
    .then(
        (results) => {          
            results.forEach((doc) => {
                console.log(doc.id);
            });
        }           
    )
    .catch(function (error) {
        console.error("Error getting user: ", error);
        response.send(error)
    });
    
    推荐文章