【发布时间】:2020-06-12 10:07:04
【问题描述】:
Firestore 新手。试图尽可能清楚。无法分配从 fisetore DB 接收到的属性, 在js中找不到答案。
建立一个问答论坛,有 3 个集合:用户、消息、查询。 尝试添加查询(在创建 msg 和用户的有效文档并获得他们的 doc id 之后)。 打印到控制台时,工作正常但不更新数据库
当遇到与数字相同的问题时,尝试使用 parseInt 进行换行并且它有效,但 parseString 似乎不存在。 还尝试只声明变量而不用“”,但随后出现错误。
function DBaddNewQuery2(userID, firstMsgId) {
let creatorFirstName =" ";
let creatorLastName=" " ;
let firstMsgText =" ";
// get the user's first and last name
db.collection("users").doc(userID).get().then(function(doc) {
creatorFirstName = doc.data().firstName;
creatorLastName= doc.data().lastName;
console.log("creatorFirstName is ",creatorFirstName); // prints the right data!
}).catch(function(error) {
console.log("Error getting first and last name for user in addquery2:", error);
});
// get msg text
db.collection("message").doc(firstMsgId).get().then(function(doc) {
firstMsgText = doc.data().msgText;
console.log("msg text is ",firstMsgText); // prints the right data!
}).catch(function(error) {
console.log("Error getting document:", error);
});
// generate query id
let queryId = create_UUID();
db.collection("query").doc(queryId).set({
'creatorFirstName': creatorFirstName, //update db as " "
'creatorLastName': creatorLastName,
'firstMsgText': firstMsgText,
}).then(function () {
console.log("setDocument: query Document successfully written!, query id: ", queryId);
})
.catch(function (error) {
console.error("setDocument: Error writing document: ", error);
});
}
【问题讨论】:
-
你是否试图连续运行一堆异步函数,而后续函数依赖于之前函数的数据?
-
是的,如果不清楚,我很抱歉
标签: javascript database google-cloud-firestore