我正在尝试在以下云函数中检索特定ID的文档。用于检索该信息的循环(请参见“阻止获取专业”),但是日志显示该函数在返回数据后正在执行该信息。 我确信这是因为这会带来新的希望,并且我需要将它们捆绑在一起。
我在这里和其他地方浏览了很多帖子,只是没有理解。
似乎我正在创建一个已兑现/被遗弃的承诺。我得到了必要的数据,但menteeMajor除外,直到稍后在日志文件中才定义该数据。
exports.getAllMatchesMCR = functions.https.onCall((data, context) => {
var dbRef = db.collection("matches");
var dbPromise = dbRef.get();
// return the main promise
return dbPromise.then(function(querySnapshot) {
var results = [];
var idx = 0;
querySnapshot.forEach(function(doc) {
// push promise from get into results
var matchObj = {
mentorName: "",
mentorEmployer: "",
mentees: []
}
var mentor = doc.data();
mentor.mentorID = doc.id;
matchObj.mentorName = mentor.mentorID;
matchObj.mentees = mentor.mentees;
for (var curIDX in matchObj.mentees) {
matchInfoObj = {};
matchInfoObj.mentorID = matchObj.mentorID;
matchInfoObj.mentorName = matchObj.mentorName;
matchInfoObj.menteeName = matchObj.mentees[curIDX];
// Block Get Major --->
var menteeRef = db.collection('users').doc(matchObj.mentees[curIDX]);
var getDoc = menteeRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
var userInfo = {};
userInfo = doc.data();
matchInfoObj.menteeMajor = userInfo.major;
// console.log('user Major:', matchInfoObj.menteeMajor);
return userInfo.major;
}
})
.catch(err => {
// console.log('Error getting document', err);
return ("No Mentee Doc")
});
console.log("in menteeInfo: ", getDoc.data());
matchInfoObj.menteeMajor = getDoc.data(); // Block Get Major <---
if (typeof something === "undefined") {
console.log('After BLOCK Major is UNDEFINED:', matchInfoObj.menteeName);
} else {
console.log('After BLOCK :', matchInfoObj.menteeMajor);
}
results.push(matchInfoObj)
}
});
// dbPromise.then() resolves to a single promise that resolves
// once all results have resolved
return Promise.all(results)
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
});
以下是两个日志的屏幕快照,它们显示了基于console.log输出的顺序。请记住,这些日志记录的类别在顶部是最新的。 Result 1 shows the start, from deployment of function and it's start, Result 2 shows the messages from the broken promise almost two minutes later.
我想做的纯文本版本是: 1.集合“用户”包含有关导师和导师的信息 2.集合“匹配”是每个Mentor(或一对多)的Mentee(数组)的文档。 3.显示一行以进行每个EACH导师/受训者连接。我有身份证,但我需要获取受训者和指导者的姓名和其他信息。
Plain old Result file shows that I am getting the rows I need with the Mentor ID, and the Mentee ID (labeled Mentees, that will change) 有人可以告诉我如何实现这一诺言吗?