Search code examples
parse-server

Handle custom response messages with Parse-server 3.0.0+


I have a project where it uses Parse-server 2.8.1 version. This was previously communicated with mLab and with the current migration to MongoDBAtlas, we have to update the parse-server version to the latest. I have seen there was a migrating doc. But there is a part which I not getting yet. How to set custom response messages. For example

        if (users.length > 0){
            response.success({
                status:false,
                message:'Screen name has already been taken.'
            });
        } else {
            response.success({
                status:true,
                message:'Screen name available.'
            });
        }

How to convert this to support the parse-server latest version. This is a must since mobile apps are communicating with the server, depending on these status values and the message.


Solution

  • It should be something like this:

    Parse.Cloud.define('myFunctionName', async () => {
      const query = new Parse.Query(Parse.User);
      // add some constraints to query
      const users = await query.find({ useMasterKey: true });
      if (users.length > 0) {
        return {
          status: false,
          message: 'Screen name has already been taken.'
        };
      } else {
        return {
          status:true,
          message:'Screen name available.'
        };
      }
    });