查询 Firebase Firestore 中的嵌套对象
我的 Firestore 数据库中有一个文档列表,其中包含此字段“参与者”作为嵌套对象。
我想进行一个查询,从数据库中获取仅一个文档(以看看它是否存在)具有(例如)用户ID 5和6。
这就是我的代码的样子
const chatsCollection = db.collection('chats');
async function createChat(myId, otherUserId){
chat = await chatsCollection
.where(`participants.${myId}`, "==", true)
.where(`participants.${otherUserId}`, "==", true)
.limit(1).get();
if(!chat.exists){
alert('chat doesnt exist')
//create chat
} else {
alert('chat exists')
//do something else
}
}
但是,即使与参与者对象的聊天确实存在于数据库中,代码的结果表明事实并非如此。
这是将数据添加到数据库时的结构,
var chat_key = (Math.random() + 1).toString(36).substring(2);
chatData = {
key: chat_key,
created_at: new Date(),
participants: {
myId: true,
otherUserId: true,
}
}
chatsCollection.doc(chat_key).set(chatData);
我感谢任何有关如何解决此问题的帮助。 谢谢 :)
I have in my Firestore database a list of documents that include this field 'Participants', as a nested object.
I want to make a query that gets only one document from the database (to see if it exists or not) that has (for example) user id 5 and 6.
This is what my code looks like
const chatsCollection = db.collection('chats');
async function createChat(myId, otherUserId){
chat = await chatsCollection
.where(`participants.${myId}`, "==", true)
.where(`participants.${otherUserId}`, "==", true)
.limit(1).get();
if(!chat.exists){
alert('chat doesnt exist')
//create chat
} else {
alert('chat exists')
//do something else
}
}
However, even if the chat with the participants object does indeed exist in the database, the result of the code indicates that it doesn't.
Here is the structure of the data when it is added to the database
var chat_key = (Math.random() + 1).toString(36).substring(2);
chatData = {
key: chat_key,
created_at: new Date(),
participants: {
myId: true,
otherUserId: true,
}
}
chatsCollection.doc(chat_key).set(chatData);
I appreciate any help on how to solve this problem.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在方法集合上,您可以使用空属性来查看查询是否正在获取数据,如果
从查询中仅获取一个文档,您可以使用
on method collection, you can use empty property to see if the query getting data or not
to get only one document from your query, you can use