Hello,
I have a prod app using Realm Sync with partitions ("_partition: user=foobar") on a M2 plan for a few months already.
Everything seems to work as expected but the server logs throw an error every day for a specific function.
I have no idea what can cause it because the log doesn’t say much and the function works 99% of the time.
Here are 2 screenshots with the errors I get. Usually it is mostly operation canceled rather than error processing request
Purpose of my function: I have an iOS game app that needs to check if the user did login today or not (in order to give him a daily reward).
So every time the user opens the game, the app runs this Realm backend function and the function will check in DB if there is a document for today. If no document found, it means the user opens for the first time “today” the game and so we save it in DB. And because we don’t want to handle seconds/minutes/hours of the day, we save it as midnight.
And here is the code.
exports = async function() {
let todayMidnight = new Date();
let realToday = new Date(todayMidnight.getTime());
todayMidnight.setHours(0, 0, 0, 0);
const user = context.user;
const realmUserId = user.id;
const identities = user.identities;
const partition = "user=" + realmUserId;
const customIdentity = identities.find(identity => identity.provider_type === "custom-function");
if (customIdentity !== undefined) {
const collection = context.services.get("mongodb-atlas").db("app").collection("LoginHistory");
const todaysLoginHistory = await collection.findOne({ _partition: partition, loginDate: todayMidnight });
if (todaysLoginHistory !== null) {
// We already logged in today
return {
realDate: realToday,
serverMidnight: todayMidnight,
insert: false
};
} else {
// First time we login today, save it in DB
const newLoginHistory = {
_id: new BSON.ObjectId(),
_partition: partition,
loginDate: todayMidnight
};
const result = await collection.insertOne(newLoginHistory);
return {
realDate: realToday,
serverMidnight: todayMidnight,
insert: true
};
}
}
// And I do the same with Sign-In With Apple identities
};
The DB scheme is very simple: _id (objectId), _partition (string), loginDate (date)