英文:
Promise returning empty value from Firestore when data is present
问题
以下是翻译好的部分:
我有一个异步函数,该函数被调用并将用户ID列表作为参数传递。该函数查询Firestore以获取在该迭代中的用户ID的令牌。
此方法如下调用:
const ids = await getIdsOfUsersWithName(name);
console.log("获取用户令牌" + ids);
const tokens = await getTokensForUsers(ids);
console.log("令牌" + tokens)
当最终的 console.log()
打印时,它打印为 "令牌,,,,,",这表明它没有返回来自 Promise 的数据,而我可以在集合中看到数据。
英文:
I have an asynchronous function that is called and a list of user ids are passed as an argument. The function queries firestore to get the token for the user id within that iteration.
async function getTokensForUsers(ids) {
const promises = ids.map(async (id) => {
console.log("Querying for user with id: " + id);
const querySnapshot = await db.collection('users').where('deviceUid', '==', id).get();
return querySnapshot.docs.map(doc => doc.data().deviceToken);
});
let tokens = await Promise.all(promises);
return tokens.flat();
}
This method is called as follows:
const ids = await getIdsOfUsersWithName(name);
console.log("Getting tokens for users" + ids);
const tokens = await getTokensForUsers(ids);
console.log("Tokens" + tokens)
When the final console.log()
prints, it prints as "Tokens,,,,," suggesting that it hasn't returned the data from the Promise, when I can see the data within the collection.
答案1
得分: 1
我已经从您提供的代码中重新创建了此问题,并获得了预期的结果。我尝试过使用firebase V9(Modular)和firebase V8(namespaced)两种方式。
这个问题似乎是在Firestore数据库集合中的deviceToken
字段为空。我建议您将这一行更改为
return querySnapshot.docs.map(doc => doc.data().deviceToken);
改成
const data = querySnapshot.docs.map((doc) => doc.data().deviceToken);
console.log(data);
return data;
这将显示每个文档的doc.data().deviceToken
返回的内容,正如您提到的,您得到了"Tokens,,,,,",这意味着您可能会得到类似这样的结果:
[ '' ]
[ '' ]
[ '' ]
[ '' ]
[ '' ]
Tokens,,,,,
这看起来像是您的deviceToken
字段为空。
以下是成功运行的代码示例:
// 初始化Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
async function getTokensForUsers(ids) {
const promises = ids.map(async (id) => {
console.log("Querying for user with id: " + id);
const querySnapshot = await db
.collection("users")
.where("deviceUid", "==", id)
.get();
const data = querySnapshot.docs.map((doc) => doc.data().deviceToken);
console.log(data);
return data;
});
let tokens = await Promise.all(promises);
return tokens.flat();
}
const name = "rohit";
async function fire() {
const ids = await getIdsOfUsersWithName(name);
console.log("Getting tokens for users: " + ids);
const tokens = await getTokensForUsers(ids);
console.log("Tokens" + tokens);
}
async function getIdsOfUsersWithName(name) { //假设
const querySnapshot = await db
.collection("users")
.where("name", "==", name)
.get();
return querySnapshot.docs.map((doc) => doc.data().deviceUid);
}
fire();
英文:
I have recreated this issue from the code you have provided and got the intended result. I have tried with both firebase V9(Modular) and with firebase V8(namespaced).
This issue seems to have the deviceToken
field being empty in the firestore database collection. I recommend you to change this line
return querySnapshot.docs.map(doc => doc.data().deviceToken);
to
const data = querySnapshot.docs.map((doc) => doc.data().deviceToken);
console.log(data);
return data;
This will show you what is being returned from the doc.data().deviceToken
of each document and as you mentioned you are getting "Tokens,,,,," which means you will probably will get something like this :
[ '' ]
[ '' ]
[ '' ]
[ '' ]
[ '' ]
Tokens,,,,
Which seems like your deviceToken
field is empty.
Here’s my code which successfully worked.:
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
async function getTokensForUsers(ids) {
const promises = ids.map(async (id) => {
console.log("Querying for user with id: " + id);
const querySnapshot = await db
.collection("users")
.where("deviceUid", "==", id)
.get();
const data = querySnapshot.docs.map((doc) => doc.data().deviceToken);
console.log(data);
return data;
});
let tokens = await Promise.all(promises);
return tokens.flat();
}
const name = "rohit";
async function fire() {
const ids = await getIdsOfUsersWithName(name);
console.log("Getting tokens for users: " + ids);
const tokens = await getTokensForUsers(ids);
console.log("Tokens" + tokens);
}
async function getIdsOfUsersWithName(name) { //assumed
const querySnapshot = await db
.collection("users")
.where("name", "==", name)
.get();
return querySnapshot.docs.map((doc) => doc.data().deviceUid);
}
fire();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论