无法在 Parse 中保存自定义类对象

Posted

技术标签:

【中文标题】无法在 Parse 中保存自定义类对象【英文标题】:Not able to save a custom class object in Parse 【发布时间】:2015-10-11 18:43:12 【问题描述】:

我正在尝试创建一个与User 类具有一对一关系的Customer 类对象。但是该对象不会在没有任何错误的情况下保存。 这是我的代码:

 Parse.Cloud.afterSave(Parse.User, function(request) 
 user = request.object;
 role_name = user.get("role_name");
 user_name = user.get("user_name");
 user_id = user.get("objectId");

  if (role_name == "customer")
    user = request.object;
    console.log(" I am inside if else");

    var Customer = Parse.Object.extend("Customer");
    var cus = new Customer();
    cus.set("name2" , "albert")
    var relation = cus.relation("userId");
    relation.add(user);
    cus.save(); // Customer object should get saved here 

    cus.save(null, 
    success: function(cus) 
        // Execute any logic that should take place after the object is saved.
        console.log("I am working")
        alert('New object created with objectId: ' + cus.objectId);
    ,
      error: function(error) 
        // handleParseError(error);
        console.log(error)
        // Execute any logic that should take place if the save fails.
        // error is a Parse.Error with an error code and message.
      
    );
  

);

运行时记录:

  after_save triggered for _User for user qu808uKOgt:
  Input: "object":"createdAt":"2015-10-11T18:36:07.661Z","objectId":"qu808uKOgt","phone":"5678956475","role_name":"customer","updatedAt":"2015-10-11T18:36:07.661Z","username":"newuser16"
  Result: Success
  I am inside if else
  "name2":"apple","userId":"__op":"AddRelation","objects":      ["__type":"Pointer","className":"_User","objectId":"qu808uKOgt"]

【问题讨论】:

你尝试使用cus.save().then(function(saveObj) console.log("I am working") ,function(error) console.log('error') ); @XenoN 我试过了,但什么也没发生 甚至没有错误日志? 这就是我得到的 - after_save triggered for _User for user qu808uKOgt: Input: "object":"createdAt":"2015-10-11T18:36:07.661Z","objectId":"qu808uKOgt","phone":"5678956475","role_name":"customer","updatedAt":"2015-10-11T18:36:07.661Z","username":"newuser16" Result: Success I am inside if else "name2":"apple","userId":"__op":"AddRelation","objects":["__type":"Pointer","className":"_User","objectId":"qu808uKOgt"] 已编辑问题与日志请检查 【参考方案1】:

我通过创建一个新的云函数来修复此错误,该函数将在用户注册后立即调用。

Parse.Cloud.define('functionName', function(request, response)
  var currentUser = Parse.User.current();
  var role_name =  currentUser.get("role_name");

  if (role_name == "customer")
  // Do something
  
  else if (role_name == "service_provider")
  // Do something else 
  
);

【讨论】:

以上是关于无法在 Parse 中保存自定义类对象的主要内容,如果未能解决你的问题,请参考以下文章

具有自定义类类型的 Swift CoreData 保存属性

无法在 android native c++ 中使用自定义类对象的向量

hashmap 自定义类键 && 对象保存/加载

通过UserNotification自定义操作访问时,CoreData无法初始化托管对象

Spring Security 自定义 UserDetailsS​​ervice 和自定义 User 类

尝试使用 NSKeyedArchiver 在 UserDefaults 中保存自定义对象