javascript - JAVASCRIPT 无法分配从 FIRESTORE 获得的变量 - 已解决:嵌套承诺很少
问题描述
Firestore 的新手。试图尽可能清楚。无法分配从 firsetore 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);
});
}
解决方案
1- async/await 可以在这里做一些技巧
async function DBaddNewQuery2(userID, firstMsgId) {
let creatorFirstName = ' ';
let creatorLastName = ' ';
let firstMsgText = ' ';
// get the user's first and last name
let userRef = await db
.collection('users')
.doc(userID)
.get();
creatorFirstName = userRef.data().firstName;
creatorLastName = userRef.data().lastName;
// get msg text
let messageRef = await db
.collection('message')
.doc(firstMsgId)
.get();
firstMsgText = messageRef.data().msgText;
// 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);
});
}
2- 嵌套结构
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(doc1) {
creatorFirstName = doc1.data().firstName;
creatorLastName = doc1.data().lastName;
console.log('creatorFirstName is ', creatorFirstName); // prints the right data!
// get msg text
db.collection('message')
.doc(firstMsgId)
.get()
.then(function(doc2) {
firstMsgText = doc2.data().msgText;
console.log('msg text is ', firstMsgText); // prints the right data!
// 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);
});
})
.catch(function(error) {
console.log('Error getting document:', error);
});
})
.catch(function(error) {
console.log(
'Error getting first and last name for user in addquery2:',
error,
);
});
}