【发布时间】:2020-03-14 18:59:56
【问题描述】:
使用 Firebase Cloud Functions 我想在文档引用数组中搜索包含某个其他文档的文档。我的结构如下所示;
Users
name
email
cars
ref to cars/car1 for example
ref to cars/car2 for example
Cars
registration
make
model
有多个用户和多辆汽车。我需要搜索在他们的汽车阵列中有特定“汽车”的用户。
我正在尝试在云函数中编写它并具有以下内容;
admin.firestore()
.collection('users')
.where('cars', 'array-contains', registration)
.get().then(doc => {
console.log("TESTING: found the user " + doc.data().email)
return
}).catch(error => {
console.error(error);
});
我知道这目前只是在数组中搜索注册字符串。无论如何要搜索特定的文档参考。我正在使用 Node.js。
用于获取在数组中具有文档引用的所有文档的工作代码;
// Notify the owner of the car
admin.firestore()
.collection('users')
.where('cars', 'array-contains', carRef)
.get().then(snapshot => {
snapshot.forEach(doc => {
console.log("TESTING found the user " + doc.data().email);
const message = {
notification: {
body: 'Your vehicle (' + carReg + ') recieved a report. Tap here to see!',
},
token: doc.data().cloudMessagingToken
};
sendMessage(message);
});
return
}).catch(error => {
console.error("Error finding a user that has the car in their garage");
console.error(error);
});
【问题讨论】:
标签: firebase google-cloud-firestore google-cloud-functions