英文:
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
答案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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论