Firebase云函数返回嵌套文档

huangapple go评论124阅读模式
英文:

firebase cloud functions return nested documents

问题

以下是您要翻译的代码部分:

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"];  

  // 我想要获取当前已登录用户(下面的'usr')的级别
  let docRef1 = admin.firestore().collection('Accounts').doc(usr);
  snapshot1 = await docRef1.get();
  doc1 = snapshot1.data();
  usr1 = doc1["Level"];

  return res.send(usr1);
});

这是您代码的翻译。

英文:

I'm using firestore database and I am trying to retrieve data from a collection, but the data is related to another document in another collection.

What I'm trying to do is the following:

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);
});

I've spent the last day just trying and trying with no luck, if I do one document it works, for example when I do this:

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);
});

It really returns the email address for the current logged in user.

why is the code above not working? what am I doing wrong ?

Any help is greatly appreciated

Thank you Firebase云函数返回嵌套文档

答案1

得分: 1

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

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

I fixed the problem, turns out it was because the 'Level' is an integer value, so I had to add toString(), like that:

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

huangapple
  • 本文由 发表于 2020年7月29日 23:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63157176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定