简体   繁体   English

无法在云中使用“数组包含”where 子句 function

[英]Unable to use 'array-contains' where clause in cloud function

I am working on a job bidding app.我正在开发一个求职应用程序。

Each user has a field "User job notifications preferences".每个用户都有一个字段“用户工作通知首选项”。

The array field stores the data to which type of job they would like to receive notifications for.数组字段存储他们希望接收通知的工作类型的数据。

for example: Person A has the setting to receive a notification when a job of type 'plumming' is created.例如:人员 A 设置为在创建“管道”类型的工作时接收通知。 Person B has the setting to receive a notification when a job of type 'electrical' is created. B 可以设置为在创建“电气”类型的作业时接收通知。

Person C creates a plumming job, Peron A should receive a notification to let them know a new job of type 'plumming' has been created.人 C 创建了一个 plumming 工作,Peron A 应该收到通知,让他们知道已经创建了一个类型为“plumming”的新工作。

here is the code snip这是代码片段

// 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);


            }


        }


    });

I think the issue is at the following line because I am getting the error 'Error: 3 INVALID_ARGUMENT: 'ARRAY_CONTAINS_ANY' requires an ArrayValue' (see image)我认为问题出在下一行,因为我收到错误“错误:3 INVALID_ARGUMENT:‘ARRAY_CONTAINS_ANY’需要一个 ArrayValue”(见图)

const usersWithJobTypePreferenceList = await admin.firestore().collection("user").where("User job notifications preferences", "array-contains-any", jobType).get();

云函数错误日志

below is the full error:以下是完整的错误:

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)

I interpret the error as the following: there is no value being passed to 'jobType'.but that cant be right because I am printing the value ( see screenshot )我将错误解释如下:没有值传递给“jobType”。但这不正确,因为我正在打印该值(见屏幕截图)

I found the following related questions but I dont think I am having the same issue:我发现了以下相关问题,但我认为我没有遇到同样的问题:

Getting firestore data from a Google Cloud Function with array-contains-any 使用 array-contains-any 从 Google Cloud Function 获取 Firestore 数据

Firestore: Multiple 'array-contains' Firestore:多个“数组包含”

So I am not sure what the issue is here, any ideas?所以我不确定这里的问题是什么,有什么想法吗?

here is how the data looks in firebase:这是 firebase 中的数据: Firebase中的数据结构

I looked at similar questions and I printed the values being passed to the function that was creating the error我查看了类似的问题,并打印了传递给造成错误的 function 的值

I updated the line that was giving me an issue now everything works:)::我更新了给我一个问题的行,现在一切正常:::

''' '''

const usersWithJobTypePreferenceList = await admin.firestore().collection("user").where("User job notifications preferences", "array-contains", jobType).get(); const usersWithJobTypePreferenceList = await admin.firestore().collection("user").where("User job notifications preferences", "array-contains", jobType).get();

''' '''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 firestore 中查询数据并遍历数组时,有没有办法使用 where() function 和“array-contains” - Is there a way to use the where() function and "array-contains" when querying data in firestore and iterate through an array Firestore:多个“数组包含” - Firestore: Multiple 'array-contains' 如何在firestore web接口中使用array-contains - How to use array-contains in firestore web interface Firebase 使用数组包含和范围过滤器进行查询 JavaScript - Firebase Query using array-contains and a range filter JavaScript Firebase array-contains - 搜索值而不是键 - Firebase array-contains - search through the values not the keys 具有高级公共/令牌规则和数组包含的 Firestore 安全规则 - Firestore Security Rules with advanced public/token rules and array-contains Firebase Firestore - 复合查询中包含多个数组 - Firebase Firestore - Multiple array-contains in a compound query 无法在 Flutter 中使用可调用云 Function - Unable to use a Callable Cloud Function in Flutter 为什么 Firestore 中数组包含的每个项目都有两个索引条目? - Why are there two index-entries for each item for array-contains in Firestore? FLUTTER:如何在cloud firestore 中使用where 子句删除多个文档? - FLUTTER: How to delete multiple documents using where clause in cloud firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM