![](/img/trans.png)
[英]Is there a way to use the where() function and "array-contains" when querying data in firestore and iterate through an array
[英]Unable to use 'array-contains' where clause in cloud function
我正在开发一个求职应用程序。
每个用户都有一个字段“用户工作通知首选项”。
数组字段存储他们希望接收通知的工作类型的数据。
例如:人员 A 设置为在创建“管道”类型的工作时接收通知。 B 可以设置为在创建“电气”类型的作业时接收通知。
人 C 创建了一个 plumming 工作,Peron A 应该收到通知,让他们知道已经创建了一个类型为“plumming”的新工作。
这是代码片段
// when a job is updated from new to open
// send notifications to the users that signed up for that jobtype notification
exports.onJobUpdateFromNewToOpen= functions.firestore
.document('job/{docId}')
.onUpdate(async (change, eventContext) => {
const beforeSnapData = change.before.data();
const afterSnapData = change.after.data();
const jobType = afterSnapData['Job type'];
const afterJobState = afterSnapData["Job state"];
const beforeJobState = beforeSnapData["Job state"];
console.log('job updated');
// only consider jobs switching from new to open
if (beforeJobState=="New" && afterJobState == "Open") {
console.log('job updated from new to open');
console.log('jobType: '+jobType);
console.log('job id: '+change.after.id )
// get users that contain the matching job type
const usersWithJobTypePreferenceList = await admin.firestore().collection("user").where("User job notifications preferences", "array-contains-any", jobType).get();
// get their userIds
const userIdsList = [];
usersWithJobTypePreferenceList.forEach((doc) => {
const userId = doc.data()["User id"];
userIdsList.push(userId);
})
// get their user tokens
const userTokenList = [];
for (var user in userIdsList) {
const userId = userIdsList[user];
const userToken = await (await admin.firestore().collection("user token").doc(userId).get()).data()["token"];
userTokenList.push(userToken);
};
// send message
const messageTitle = "new " + jobType + " has been created";
for (var token in userTokenList) {
var userToken = userTokenList[token];
const payload = {
notification: {
title: messageTitle,
body: messageTitle,
sound: "default",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
message: "Sample Push Message",
},
};
return await admin.messaging().sendToDevice(receiverToken, payload);
}
}
});
我认为问题出在下一行,因为我收到错误“错误:3 INVALID_ARGUMENT:‘ARRAY_CONTAINS_ANY’需要一个 ArrayValue”(见图)
const usersWithJobTypePreferenceList = await admin.firestore().collection("user").where("User job notifications preferences", "array-contains-any", jobType).get();
以下是完整的错误:
Error: 3 INVALID_ARGUMENT: 'ARRAY_CONTAINS_ANY' requires an ArrayValue.
at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:352:49)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:328:181)
at /workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:188:78
at processTicksAndRejections (node:internal/process/task_queues:78:11)
我将错误解释如下:没有值传递给“jobType”。但这不正确,因为我正在打印该值(见屏幕截图)
我发现了以下相关问题,但我认为我没有遇到同样的问题:
使用 array-contains-any 从 Google Cloud Function 获取 Firestore 数据
所以我不确定这里的问题是什么,有什么想法吗?
我查看了类似的问题,并打印了传递给造成错误的 function 的值
我更新了给我一个问题的行,现在一切正常:::
'''
const usersWithJobTypePreferenceList = await admin.firestore().collection("user").where("User job notifications preferences", "array-contains", jobType).get();
'''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.