Skip to content

Cloud code save object doesn't work #3399

Closed
@rusmichal

Description

@rusmichal

Issue Description

I use promises to create Balance object for each new user but it seems to balance.save(..) doesn't work.

Line: balancePromise.push(balance.save(null, { useMasterKey: true }));

Describe your issue in as much detail as possible.

Maybe from beginning.:

  1. query object call to check if balance has already exist.
  2. If there isn't I create 'Admin' role query and push adminQuery into adminPromises array and return call with that promieses. (Balance can change onlny admin)
  3. When I get role I create balance and push that operation into balancePromise array and call
    Parse.Promise.when(balancePromise) and after this I got nothing. Take a look on logs.

****** balance log is the last before save balance. After this got nothing, no errors etc.

nfo: afterSave triggered for _User for user DtTd6iR3TU:
2017-01-19T12:39:15.894297+00:00 app[web.1]:   Input: {"email":"[email protected]","username":"[email protected]","profile_picture_url":"none","createdAt":"2017-01-19T12:39:15.483Z","updatedAt":"2017-01-19T12:39:15.878Z","ACL":{"*":{"read":true},"DtTd6iR3TU":{"read":true,"write":true}},"system":"android","version":"ALE-L21, 23  6.0, staging-2.5","objectId":"DtTd6iR3TU"} className=_User, triggerType=afterSave, user=DtTd6iR3TU
2017-01-19T12:39:15.930441+00:00 app[web.1]: ***** Found role
2017-01-19T12:39:15.930812+00:00 app[web.1]: ****** balance2 ParseObject { _objCount: 14, className: 'Balance' }
2017-01-19T12:39:15.930848+00:00 app[web.1]: ****** acl
2017-01-19T12:39:15.930930+00:00 app[web.1]: ****** balance
2017-01-19T12:39:15.942114+00:00 app[web.1]: user {"triggerName":"beforeSave","object":{"ACL":{},"owner":"DtTd6iR3TU","currency":"pln","amount":0},"master":true,"log":{"appId":"DOo6g0IS8NXyv6AP2SV59ehXaVX14XZXcXqukvlZ"},"installationId":"8b4ba0b8-9ad1-b28d-5908-204bb9187deb"}

If I remove balance.save(..) then next promises are called.

Steps to reproduce

Parse.Cloud.afterSave(Parse.User, function (request) {
    var user = request.object.id;
    
    var query = new Parse.Query("Balance");
    query.equalTo("owner", user);

    var adminQuery = new Parse.Query(Parse.Role);
    adminQuery.equalTo("name", "Administrator");

    var clientQuery = new Parse.Query(Parse.Role);
    clientQuery.equalTo("name", "Client");

    var clientPromises = [];
    var adminPromises = [];
    var balancePromise = [];

    query.first({ useMasterKey: true }).then(function(object) {
        if (typeof object === 'undefined') {
            adminPromises.push(adminQuery.first({useMasterKey: true}));
        } else {
            console.log("Balance already exists");
        }

        clientPromises.push(clientQuery.first({useMasterKey: true}));

        return Parse.Promise.when(adminPromises);
    }).then(function(role) {
        console.log('***** Found role'); 

        if(role) {
            var balance = new Parse.Object("Balance");
            balance.set("owner", user);

            //TODO parametrize it later
            balance.set("currency", "pln");
            balance.set("amount", 0);

            console.log('****** balance2', balance);
            var acl = new Parse.ACL();

            /*acl.setPublicReadAccess(false);
            acl.setPublicWriteAccess(false);
            acl.setReadAccess(user, true);
            acl.setReadAccess(role, true);
            acl.setWriteAccess(role, true);*/

            console.log('****** acl');

            balance.setACL(acl);
            console.log('****** balance');

            balancePromise.push(balance.save(null, { useMasterKey: true }));
        } else{
            console.log('errorrrrrr');
            status.error(error);
            //response.error("Failed " + error.code + ". " + error.message);
        }
 
        return Parse.Promise.when(balancePromise);    // nothing get after this
    }).then(function (success) {
        console.log('****** rolesssss');
        return Parse.Promise.when(rolePromises);
    }).then(function (object) {
        console.log('****** objectssssss', JSON.stringify(object));
        
        console.log('****** object', JSON.stringify(object));
        console.log('create relation');    // didn't appear
        object.relation("users").add(request.object.id);
        return object.save(null, { useMasterKey: true });
    }).then(function(result) {
        console.log("Successfullyyyyyyyyyyyy");
        response.success("Successfully created user with balance");
    }, function(error) {
        console.log("Erorrrrrrrrrr" + JSON.stringify(error));
        response.error("Could not save changes to user!" + error);
    });
});

Expected Results

Save balance object and call next steps of promises chain.

Actual Outcome

Cannot save balance object.

Environment Setup

  • Server

    • parse-server version : 2.3.1
    • Operating System: Heroku Linux
    • Hardware: Heroku Dyno
    • Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): Heroku
  • Database

    • MongoDB version: 3.2.11
    • Storage engine: Unknown
    • Hardware: Unknown
    • Localhost or remote server? (AWS, mLab, ObjectRocket, Digital Ocean, etc): mLab

Logs/Trace

You can turn on additional logging by configuring VERBOSE=1 in your environment.

Logs above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions