简体   繁体   English

如何从 firebase 中的引用文档中检索数据?

[英]how can i retrieve data from a referenced document in firebase?

I'm relatively new to firebase, and I'm trying to retrieve data from a referenced document in another collection to show up.我对 firebase 比较陌生,我正在尝试从另一个集合中的引用文档中检索数据以显示。 https://drive.google.com/file/d/1CcGCPgO9JVd6iCA_1FHfO95J2_E19Z0I/view?usp=sharing https://drive.google.com/file/d/1tM5g_A8jcfKTMWMczaS_35gjNcDn_Ho1/view?usp=sharing https://drive.google.com/file/d/1CcGCPgO9JVd6iCA_1FHfO95J2_E19Z0I/view?usp=sharing https://drive.google.com/file/d/1tM5g_A8jcfKTMWMczaS_35gjNcDn_Ho1/view?usp=sharing

this is how i add a student and the parent's ID这就是我添加学生和家长 ID 的方式

const newStudent = {
    name: req.body.name,
    grade: req.body.grade,
    //status: `no parent request`,
    parentId:db.doc(req.body.parentId),
    gender:req.body.gender,
    //parentName:req.body.parentName,
    transporation:req.body.transporation,
    userHandle: req.user.handle
  };

what I'm trying to do is retrieve the name of the parent I'm referencing in the student document, I tried the following but it gave me the entire snapshot of the parent, i tried a lot of methods but none of them work我想要做的是检索我在学生文档中引用的父母的名字,我尝试了以下但它给了我父母的整个快照,我尝试了很多方法,但没有一个有效

(req, res) => { db.collection("students")
   [.orderBy("grade", "desc")
    .get()
    .then((data) => {
     let students = \[\];
     //const {parentname} = db.collection('parent').doc.data().name;
      data.forEach((doc) => {
      var parentId = doc.data().parentId.id;][1]
  
  db.collection("parent").get().then((pdata) => {
    pdata.forEach((pdoc) => {
      students.push({
        parentname: parentId});});});
  students.push({
    studentId: doc.id,
    name: doc.data().name,
    grade: doc.data().grade,
    //parentName:parentname
    // parentName:doc.data().parentId.id,
    
  });
});
return res.json(students); })

any advice or help is much appreciated.非常感谢任何建议或帮助。

Here what you are doing is, you are calling the whole data inside the document you are calling.您在这里所做的是,您正在调用您正在调用的文档中的整个数据。
So you have to explicitly call the particular object of the document.因此,您必须显式调用文档的特定 object。

//TODO: change the docName to desired document id
db.collection("parent").doc('docName').collection('name').get().then((pdata) => {
    pdata.forEach((pdoc) => {
      students.push({
        parentname: parentId});});});
  students.push({
    studentId: doc.id,
    name: doc.data().name,
    grade: doc.data().grade,
    //parentName:parentname
    // parentName:doc.data().parentId.id,
    
  });

Here I am referring to the particular document of which the data is to be retrieved.这里我指的是要检索其数据的特定文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM