Custom objectId in cloud code function

Hey, perhaps only to confirm as I already saw this old and closed issue…

It is not implemented to save an object in the cloud code with a customId? I am following the example in the documentation to keep track of saved files in the storage and I was thinking about using the saved file name as a custom objectId as it is already a unique name and I would need to index the name field anyway.

My code to save the object in afterFileSave trigger is:

Parse.Cloud.afterSaveFile(async (request) => {
    const { file, fileSize, user } = request;
    const name = file.name();
    request.log.info(`file saved: ${name}`);    
    const fileObject = new Parse.Object('FileObj');
    fileObject.set('objectId', name);
    fileObject.set('file', file);
    fileObject.set('size', fileSize);
    fileObject.set('cb', user.id);
    const token = { sessionToken: user.getSessionToken() };
    await fileObject.save(null, token);
});

from console I can see the urls and can also open the saved files, they are being saved successfully. But in the afterFileSave trigger I get an error ParseSwift.ParseError.Code.fileSaveFailure, message: "Object not found."

I assume this is because it is not possible to set customId as it is not implemented in JS SDK? Or are there any forbidden character like “.”, “_”,… for the objectId?

Thank you for confirmation!

To answer part of the question, the JS SDK has custom objectId implemented here:

If you look towards the bottom of that thread, it looks like some users are having the issue you are having with cloud code. Also, someone mentioned that the last released server does not contain the JS SDK version that contains customObjectId, feat: Allow saving with custom objectId by dplewis · Pull Request #1309 · parse-community/Parse-SDK-JS · GitHub

It has test cases, but I’ve never used it in JS directly so I’m not able to answer the rest.

1 Like

Where would be the best place to put Parse.allowCustomObjectId = true; in the cloud code then ?

If I call it inside any cloud trigger, it will get called many times and the server needs it only once I believe. That would be unnecessary load, am I right? I am trying to create ParseObject in afterSaveTrigger of other ParseObject but even I set createdAt to null I still get:

Parse error: Object not found.