简体   繁体   English

Parse.com将用户指针添加到保存之前的类中

[英]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. 我有一个名为Roster的表,其中将用户指针存储在该列之一中。 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 我正在尝试在新方法之前在保存方法之前设置该列。到目前为止,我有这个但我不确定如何获取用户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. Todd的答案适用于客户端,但不适用于发生beforeSave触发的云代码。

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. 如果您需要访问用户ID以外的任何用户信息,则必须先获取用户,因为请求中不会发送整个对象。

Edit - Just wanted to add that before/afterSave triggers have a 3 second timeout. 编辑-仅想添加在保存触发之前/之后有3秒的超时。 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. 这足够执行一两个快速查询的时间,但是如果数据库中有很多对象,或者执行许多保存/获取/查询调用,最终可能会超过3秒的限制。 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 正如Jake T所指出的,您将需要使用

var user = request.user

Parse.User.current() is not supported in a cloud code environment. 云代码环境中不支持Parse.User.current()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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