Hello there, I’m running into a pretty weird case which has gotten my head spinning lately. Here’s the scenario: I’m creating a chat application, and when the user opens the app, I want to update the messages he’s missed when offline for each room. So I created this cloud function and here’s the request data: a javascript object, with roomIds as the keys and last updated date for that room as the value. like :
{
21: Sun Jan 21 2024 10:36:16 GMT+0330 (Iran Standard Time),
41: Sun Jan 21 2024 10:51:28 GMT+0330 (Iran Standard Time)
}
and I’m willing to process data like this as a cloud function :
Parse.Cloud.define("missedMessages", async (request) => {
let result = {};
const data = request.params;
const queries = [];
async function calculate() {
Object.keys(data).map(async (room) => {
const query = new Parse.Query("messages");
query.fullText('roomId', room);
if (data[room]) {
const date = new Date(data[room]);
var utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(),
date.getUTCDate(), date.getUTCHours(),
date.getUTCMinutes(), date.getUTCSeconds());
query.greaterThan('createdAt', new Date(utc));
}
const response = await query.find();
result[room] = response;
//queries.push(query);
})
}
// const finalQuery = Parse.Query.or(queries);
// const result = await finalQuery.find();
// await calculate();
return result;
});
The current code simply returns an empty object {}
. So I suspected the problem might be related to querying each data individually - all those async and await. As you can see in commented code, I removed calculate
wrapper function and synchronized the function passed to map, pushed queries in an array queries
, so then I can stack them up in one single query through Parse.Query.or
. But apparently it doesn’t work(Throws this error : A ParseQuery must be constructed with a ParseObject or class name.
), though the queries
array is constructed correctly.
So to sum it up, I go with a few questions.
-
Can I stack an array of queries in cloud code, instead of this
Parse.Query.or(query1, query2, ...)
-
It seems like the
calculate
async wrapper function is skipped, despite the await operator. Is it possible to define and call another function inside a cloud code function? -
And finally, any thoughts on what’s going on?! Or basically a better practice than I mentioned for the task(perhaps a query shortcut or something?)
Thank you all.
UPDATE :
I also tried Parse.Query.or(...queries)
. Must answer my first question but still returns an error : Server 500 Uncaught (in promise) Error: [object Object]
and the response looks like this {"code":1,"error":{"ok":0,"code":2,"codeName":"BadValue"}}
UPDATE :
This sounds like a bug. Because every query is fine individually and will return desired data. But
await Parse.Query.or(...queries).find();
returns the following error, which apparently has got something to do with mongodb.
{"code":1,"error":{"ok":0,"code":2,"codeName":"BadValue"}}