Search code examples
javascriptasync-awaitback4app

Parse cloud code js using await throws syntax error


My issue is this, I'm using Parse cloud code and I have a before save trigger, like this:

Parse.Cloud.beforeSave("Ordenes", function(request, response) {
    console.log(request.object + " beforeSaveCalled");
    var query = new Parse.Query("Ordenes");
    var current = query.get(request.object.id, {
    success: function(object) {
        console.log(object + "This row");

        console.log(object.id + " object " + request.object.id + " request.object " + "exist?");

        if (object.get("MozoAdded") === false ) {
            console.log("correct return we can proceed");
//          const ordenLocation = object.get("Ubicacion");
//          const queryMozos = new Parse.Query("MozosLocation");
//          
//          queryMozos.near("MozoLocation1", ordenLocation);
//          
//          queryMozos.limit(1);
//          
//          const mozoToAdd = await queryMozos.find();
//          
//          console.log(mozoToAdd +" El mozo cerca es " + mozoToAdd[0].get("Name"));

//          object.set("MozoName", mozoToAdd[0].get("Name"));


            response.success();
        } else if (object.get("MozoAdded") === undefined) {
            console.log("Weird but not incorrect, proceed");
//          const ordenLocation = object.get("Ubicacion");
//          const queryMozos = new Parse.Query("MozosLocation");
//          
//          queryMozos.near("MozoLocation1", ordenLocation);
//          
//          queryMozos.limit(1);
//          
//          const mozoToAdd = await queryMozos.find();
//          
//          console.log(mozoToAdd +" El mozo cerca es " + mozoToAdd[0].get("Name"));
//          
//          object.set("MozoName", mozoToAdd[0].get("Name"));
            response.success();
        } else {
            console.log("Big nope, nope, nope");
            response.error("Esta orden ya fue asignada.");

        }
    },

    error: function(object, error) {
        response.success();

    }
    });

});

With the commented code uncommented is throwing this error:

 Cloud Code not loaded:
 /usr/src/app/data/cloud/main.js:175
            const mozoToAdd = await queryMozos.find();
                                    ^^^^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/src/app/cloudCodeWrapper.js:4:20)

What i want to do is find the nearest mozo in the MozosLocation table, using the Orden["Ubicacion"] to add its name to the object I'm trying to save.

I'm im not really familiar with js as you can see, and parse guides don't really cover this type of errors, so any ideas on what it could be causing it? Help


Solution

  • It's available only at the Parse Server Version 3.0.0. Only this version fully supports promises and async/await, as you can see in its releases.