首页 > 解决方案 > firebase 云函数返回嵌套文档

问题描述

我正在使用 firestore 数据库,我正在尝试从集合中检索数据,但数据与另一个集合中的另一个文档相关。

我想要做的是以下几点:

exports.acc = functions.https.onRequest(async (req, res) => {
  let docRef = admin.firestore().collection('LoggedIn').doc('CurrentLogin');
  snapshot = await docRef.get();
  doc = snapshot.data();
  usr = doc["Email"];  
  
// I want to get the Level from the Current Logged In user (the 'usr' below)
  let docRef1 = admin.firestore().collection('Accounts').doc(usr);
  snapshot1 = await docRef1.get();
  doc1 = snapshot1.data();
  usr1 = doc1["Level"];
  
  return res.send(usr1);
});

我花了最后一天尝试,但没有运气,如果我做一个文档,它就可以工作,例如当我这样做时:

exports.acc = functions.https.onRequest(async (req, res) => {
  let docRef = admin.firestore().collection('LoggedIn').doc('CurrentLogin');
  snapshot = await docRef.get();
  doc = snapshot.data();
  usr = doc["Email"];  
  
  return res.send(usr);
});

它确实返回了当前登录用户的电子邮件地址。

为什么上面的代码不起作用?我究竟做错了什么 ?

任何帮助是极大的赞赏

谢谢 :)

标签: javascriptjavajsonfirebase

解决方案


我解决了这个问题,原来是因为'Level'是一个整数值,所以我不得不添加toString(),就像这样:

usr1 = doc1["Level"].toString();

推荐阅读