Code syntax in 4.3.0

I just updated my parse server from 2.8.4 to 4.3.0 and I understand there have been changes in how the code is now written.

So the code below…

Parse.Cloud.define("updateCourseFunction", func (request, response) {
    const currentUserId = request.user.id;
    const courseId = request.params.courseId;

    // ---------------------------------

    courseObject.save(null, {
        useMasterKey: true,
        success: function(savedCourseObject) {
             if (savedCourseObject != null)
                 response.success(savedCourseObject);
             else
                 response.success();
             },
             error: function(error) {
                 response.error(error);
             }
    });
});

should be like this?

Parse.Cloud.define("updateCourseFunction", async (request) => {
    const currentUserId = request.user.id;
    const courseId = request.params.courseId;

    // ---------------------------------

    try {
        const savedCourseObject = await courseObject.save(null, { useMasterKey: true  });
        
        if (savedCourseObject != null)
            return savedCourseObject;
        else 
          return;
    } catch (error) {
        throw error;
    }
});

Yes async function style is the correct syntax !
This should works perfectly !

1 Like

Great! So in this case;

if (savedCourseObject != null)
    return savedCourseObject;
else <==== 
    return;

What would the client get as a response? null?

I don’t think client get null response, I mean if you want to, you can send null response. But By looking your code, İf object saved successfully, savedCourseObject will never be null. And if error happens while saving, catch block will run and client will get an error.

2 Likes

What if I run a find request and the query finds no objects? Will it succeed and return an empty array, or will it throw an error?

It will return empty array

1 Like