Javascript Firebase Firestore.get()未返回预期数据
此代码段始终返回“继续!”:Javascript Firebase Firestore.get()未返回预期数据,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,此代码段始终返回“继续!”: firebase.firestore().collection(“users”).doc(user.uid).collection(“scores”).doc(“test”).get(“testscores”)和firebase.firestore().collection(“users”).doc(user.uid).get(“target”)都应该是firestore中看起来像数字的字符串(例如字符串“1000”)。我想使用parseInt()将这两个数据字段作
firebase.firestore().collection(“users”).doc(user.uid).collection(“scores”).doc(“test”).get(“testscores”)
和firebase.firestore().collection(“users”).doc(user.uid).get(“target”)
都应该是firestore中看起来像数字的字符串(例如字符串“1000”)。我想使用parseInt()
将这两个数据字段作为整数,并对它们执行比较,但它总是会发出“继续”警报,即使它不应该发出警报
此外,我还尝试通过以下方式进行调试:
console.log(firebase.firestore().collection("users").doc(user.uid).collection("scores").doc("test").get("testScore"));
console.log(firebase.firestore().collection("users").doc(user.uid).get("target"));
console.log("......");
console.log(scoreAsInt);
console.log(targetScoreAsInt);
控制台返回了以下内容:
承诺{uu:0,{uv:0,{uw:null,{ux:null,}
承诺{uu:0,{uv:0,{uw:null,{ux:null,}
楠
楠
尝试检索Firestore数据以执行比较,我在这里做错了什么?Firestore返回承诺,因此您需要将其放入支持异步/等待或使用承诺的函数中。另外,通过两次获取同一文档,您将花费2次读取 正在使用异步/等待
async function getScores() {
try {
const db = firebase.firestore();
const scoreDoc = await db.doc(`users/${user.id}/scores/test`).get();
const {testScore, target} = scoresDoc.data();
const scoreAsInt = parseInt(testScore);
const targetAsInt = parseInt(target);
if(scoreAsInt === targetScoreAsInt || scoreAsInt > targetScoreAsInt) {
window.alert("You reached your target!!");
} else {
window.alert("Keep going!");
}
} catch (err) {
console.error(err);
}
}
…或者承诺
const db = firebase.firestore();
return db.doc(`users/${user.id}/scores/test`).get().then((scoresDoc) => {
const {testScore, target} = scoresDoc.data();
const scoreAsInt = parseInt(testScore);
const targetAsInt = parseInt(target);
if(scoreAsInt === targetScoreAsInt || scoreAsInt > targetScoreAsInt) {
window.alert("You reached your target!!");
} else {
window.alert("Keep going!");
}
}).catch((err) => {
console.error(err);
});
Firestore返回承诺,因此您需要将其放入支持异步/等待或使用承诺的函数中。另外,通过两次获取同一文档,您将花费2次读取 正在使用异步/等待
async function getScores() {
try {
const db = firebase.firestore();
const scoreDoc = await db.doc(`users/${user.id}/scores/test`).get();
const {testScore, target} = scoresDoc.data();
const scoreAsInt = parseInt(testScore);
const targetAsInt = parseInt(target);
if(scoreAsInt === targetScoreAsInt || scoreAsInt > targetScoreAsInt) {
window.alert("You reached your target!!");
} else {
window.alert("Keep going!");
}
} catch (err) {
console.error(err);
}
}
…或者承诺
const db = firebase.firestore();
return db.doc(`users/${user.id}/scores/test`).get().then((scoresDoc) => {
const {testScore, target} = scoresDoc.data();
const scoreAsInt = parseInt(testScore);
const targetAsInt = parseInt(target);
if(scoreAsInt === targetScoreAsInt || scoreAsInt > targetScoreAsInt) {
window.alert("You reached your target!!");
} else {
window.alert("Keep going!");
}
}).catch((err) => {
console.error(err);
});
嘿,所以我刚刚做了
scoresDoc.data()[“testScore”]
,我得到了我需要的,谢谢!这也行得通。使用Promissions或Async/Await修复您的问题没有那么有效?如果是的话,请你把我的答案标记为已接受?嘿,我刚刚做了scoresDoc.data()[“testScore”]
,我得到了我需要的,谢谢!这也行得通。使用Promissions或Async/Await修复您的问题没有那么有效?如果是这样的话,请你把我的回答标记为接受?