简体   繁体   中英

Parse.com Add User Pointer to a class in before save

I have a table called Roster where it stores a User Pointer in one of the column. I am trying set that column in before save method when it is new.So far i have this but i am not sure how to get the user id

Parse.Cloud.beforeSave("Roster",function(request,response){
  //Update roster with sender Id
  if (request.object.isNew()){
    var userPointer = /*NOT SURE HOW TO GET WHO SENT IT*/
    request.object.set("User",userPointer);
  }
   response.success(); 
});

Any idea?

var userPointer = request.user;

Is the proper method. Todd's answer works on the client side, but not on cloud code, where beforeSave triggers occur.

If you need to access any of the user's information beyond their id, you'll have to first fetch the user, as the entire object is not sent in the request.

Edit - Just wanted to add that before/afterSave triggers have a 3 second timeout. This is enough time to perform a quick query or two, but if you have a lot of objects in your database, or perform many save/fetch/query calls, you may end up exceeding your 3 second limit. If you have a lot of that logic that needs to occur, rather than saving the object from the client, call a cloud code function that handles all of those changes, then saves the object and returns the newly saved object, so that you can set your client side object to the returned, up to date object.

As Jake T pointed out, you will need to use

var user = request.user

Parse.User.current() is not supported in a cloud code environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM