Parse Cloud code does not function as intended in back4app

1,563 views
Skip to first unread message

pet...@frogsy.com

unread,
May 11, 2016, 9:24:14 AM5/11/16
to back{4}app
Hi all, 

Today we've tried experimenting with back4app migration tools, to migrate our parse DB. Everything went smoothly to say at least, except the cloud code functions.

We imported our parse "main.js" file. The calls from our app to the the main.js functions are successful, however, the "object.set()" and "object.save()" functions do not.

I'll post a sample code below so that you can take a look and let me know whether you have come across something similar lately.

Now, we may have missed something in the tutorials but since we can't access the back4app Blog today, we'll just try this group.

Thanks in advance. We're so eager in hearing back from you.


A function in main.js:
---------------

Parse.Cloud.define("whateverName", function(request,response) {
      
      var user = new Parse.User();
      var query = new Parse.Query(Parse.User);

      query.equalTo("objectId", request.params.objectId);
      query.first({
            success: function(object) {

                  var logInfo = "\n what = "+ request.params.what;
                  object.set("what", request.params.what);
                  object.save();

                  console.log(logInfo);
                  response.success({Data:"Done"});
            },
            error: function(error) {
                  response.error("WhateverName[" + error.code + "]: " + error.message + "");
            }
      });
});


---------------
Console Logs:
--------------
what = 1

But the 'request.params.what' value that was supposed to be set in "what" column in parse DB is never set. It just stays "undefined".

pet...@frogsy.com

unread,
May 12, 2016, 10:01:35 AM5/12/16
to back{4}app
so back4app development team suggested changing the .save() code to:

object.save({
  useMasterKey: true,
  success: function () {
     console.log(logInfo);
     response.success({data:"Done"});
 },
  error: function (error) {
     response.error("error on save");
  }
});

However, it still did not work, so we had to add the  "Parse.Cloud.useMasterKey();" on top of the object.save block. Like below:

Parse.Cloud.useMasterKey();
object.save({
  useMasterKey: true,
  success: function () {
     console.log(logInfo);
     response.success({data:"Done"});
 },
  error: function (error) {
     response.error("error on save");
  }
});

However, Parse clearly states that 
"Parse.Cloud.useMasterKey() is not available in Parse Server Cloud Code. Instead, pass useMasterKey: true as an option to any operation that requires the use of the master key to bypass ACLs and/or CLPs.".

Therefore, if anyone knows why we actually need to use it in order for it to work, please let us know :) Also, what's the complications for using it in the cloud code, since we never needed to use it on Parse.


In addition, to anyone facing any code failing issues similar to this or otherwise, please note that if you use "alert()" functions anywhere in your code, they do not work and the code fails. So instead, please use console.log().


Thanks.



Davi Macêdo

unread,
May 12, 2016, 8:02:54 PM5/12/16
to back{4}app
Hi Petros.

Let me understand better. The code you sent above is running on Server Side (cloud functions) or Client Side (your device)?

Best.

pet...@frogsy.com

unread,
May 13, 2016, 7:34:26 AM5/13/16
to back{4}app
Hi Davi,

It's server side cloud functions.

Well, talking with back4app support, we've changed the above code to the one below and now there's no need to use the Parse.Cloud.useMasterKey(). However, we are still facing the issue of the OAuth data not being set at all in the Parse-Server DB for some reason. 
We've followed back4app's guide in adding the Facebook AppID in the "Facebook OAuth Settings" tab. Everything works fine, but the authData columns are not populated. We need that information for other functions, and I'm still researching as to what the issue might be.
Any ideas?


**new code:

object.save(null, { useMasterKey: true }).then(
                        function() {
                              console.log("Object Saved Successfully"); 
                              response.success({data:"Done"});
                        }, 
                        function(error) {
                              console.log("Error Saving Object -- Response Error");        
                              response.error("[Error Code]: " + error.code + "[Error Message]: " + error.message);
                        }
                  );

Davi Macêdo

unread,
May 17, 2016, 2:13:53 PM5/17/16
to back{4}app
Hi, Petros. Thank you for the updated code and information. About oauth column, it is stored in the database but there is a bug in Parse Dashboard open source that is not showing the information. If you connect to your database with a mongo client you will see the data is there. This issue is in our task list. If it will not be solved to the community, we will take care of it soon. Best!

pet...@frogsy.com

unread,
May 18, 2016, 2:30:53 AM5/18/16
to back{4}app
Hi Davi, 

Thank you so much for your reply. We were going crazy over this issue :)

However, I want to add some more information regarding this issue. In our server side cloud code we use something like this:

var userAuth = object.get("authData");
console.log(userAuth);

console.log(fbID);

And it returns "undefined" on both instances. That means, it's not saved at all, isn't it?


Davi Macêdo

unread,
May 18, 2016, 5:01:58 AM5/18/16
to back{4}app
Hi Petros. I think that data structure changed little bit in Parse Server and that's why Parse Dashboard has the bug. But the data is there. You can see it in your database :)

Please, try the code below:
var fbID=object.get("_auth_data_facebook").id

Best!

pet...@frogsy.com

unread,
May 18, 2016, 6:46:51 AM5/18/16
to back{4}app
Hi again.

Thank you for the code. We tried that but still _auth_data_facebook returns undefined.

We have connected using a mongodb client. We can see that the field _auth_data_facebook is populated but for some reason 'object.get("_auth_data_facebook")' returns undefined... 
Same happens with the updatedAt field, which is now changed to _updated_at.

Davi Macêdo

unread,
May 22, 2016, 8:15:54 PM5/22/16
to back{4}app
Hi Petros. I did some other trials, but it is really not returned in the actual version of Parse Server. We will help the community in this issue. Best!

pet...@frogsy.com

unread,
May 23, 2016, 7:49:03 AM5/23/16
to back{4}app
Hi Davi,

Thanks for the reply!

Is there a rough estimation as to when this feature might/will be available, for us developers to use?

Again thank you!

Davi Macêdo

unread,
May 23, 2016, 2:55:02 PM5/23/16
to back{4}app
Probably it will be fixed in the next update by the end of the week.

pet...@frogsy.com

unread,
May 24, 2016, 3:08:45 AM5/24/16
to back{4}app
Brilliant! Thank you! :)

Davi Macêdo

unread,
May 26, 2016, 6:12:49 PM5/26/16
to back{4}app
Hi, Petros.

Can you try now using user.get('fbId') ?

Best!

pet...@frogsy.com

unread,
May 27, 2016, 8:23:17 AM5/27/16
to back{4}app
Yes! It works now! Thank you! :)


Davi Macêdo

unread,
May 27, 2016, 11:50:07 AM5/27/16
to back{4}app
You're welcome!

aqee...@gmail.com

unread,
Sep 9, 2016, 3:27:53 PM9/9/16
to back{4}app
Hi Petros, 

Could you please post the final working code snippet as a reference here as I'm having the same problem?

Thanks

Davi Macêdo

unread,
Sep 10, 2016, 5:58:40 PM9/10/16
to back{4}app
@aweel.ran, could you please paste here your current code? Best.

thomas....@gmail.com

unread,
May 15, 2017, 8:57:54 PM5/15/17
to back{4}app
Hi Davi, I'm having the same issue. 

I can't change a simple field on User Class on Back4app. However, running locally or in amazon AWS, the same code works fine.

exports.setBusy = function(user) {
var u = new Parse.User();
u.id = user.id;
u.set("busy", true);
return user.save(null ,{useMasterKey:true});
}

Could you help me please?

Thank you! 

Davi Macêdo

unread,
May 15, 2017, 9:13:15 PM5/15/17
to back{4}app, thomas....@gmail.com
Hi, Thomas.

What is the error you have?

Best!

thomas....@gmail.com

unread,
May 15, 2017, 9:18:28 PM5/15/17
to back{4}app, thomas....@gmail.com
Thanks for replying so fast :) 

That's the weirdest part. It doesn't generate any error, it just doesn't update the user.

It returns the user normally, but when I verify the information I'm trying to update, it shows "undefined".

thomas....@gmail.com

unread,
May 15, 2017, 9:26:35 PM5/15/17
to back{4}app, thomas....@gmail.com
In case this helps, I'm sending the rest of the code and the stack trace generated on back4app.


Parse.Cloud.define("testBusy", function(request, response) {

var u = new Parse.User();
    u.id = request.user.id;

user.setBusy(u).then(function (result) {
console.log(result);
console.log(result.get("busy"));
response.success(result);
});

});

casag...@back4app.com

unread,
May 16, 2017, 12:44:47 PM5/16/17
to back{4}app, thomas....@gmail.com
Hello Thomas!

It seems that the Online Chat Support has solved your issue, is that correct?

Do you need anymore help? If you do, please contact us.

Best!

thomas....@gmail.com

unread,
May 16, 2017, 12:56:14 PM5/16/17
to back{4}app, thomas....@gmail.com
Hello!

Yes, that's correct. 

That's all I needed, thank you! 
Reply all
Reply to author
Forward
0 new messages