在解析中保存基本关系 [英] Saving a basic relation in Parse

查看:110
本文介绍了在解析中保存基本关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个例子很简单,但是并不奇怪,它没有用. mLate是Late表中的一个对象或对象数组. 我的代码:

The example was straight forward but not surprisingly, it is not working. mLate is an object or array of objects from the Late table. My code:

var user = Parse.Object.extend("User"); 
var userQuery = new Parse.Query(Parse.User);        
var Late = Parse.Object.extend("Late");        
var lateQuery = new Parse.Query(Late);
userQuery.get("yCDDEWoiwM", {//this is where the userID goes
     success: function (userDetails) {
           var relation = userDetails.relation("mLate");
           relation.add(mLate);
           userDetails.save(); 

鉴于Parse SDK,我无法想象出哪里出问题了.我不断 POST https://api.parse.com/1/classes/_User/yCDDEWoiwM 400(错误请求)

Given the Parse SDK, I can't imagine where I'm going wrong. I keep getting POST https://api.parse.com/1/classes/_User/yCDDEWoiwM 400 (Bad Request)

新的假设和更多的代码

我知道这确实很简单,而且我很生气Parse,因此很少在他们的解释中添加任何细微的细节.您是否看过他们的API(好,我不想上肥皂盒……)

I know this is something really simple and I'm really pissed that Parse so rarely adds even slight detail to what they explain. Have you seen their API (OK, I don't want to get on my soap box . . .)

我的假设是我多次从Late表中转换了Parse.Object,而Parse不再以某种方式知道那里发生了什么.

My hypothesis is that I transformed the Parse.Object from the Late table several times and that somehow Parse no longer knows what's going on there.

下面是对代码的更完整的介绍:

Here's a more complete look at the code:

var user = Parse.Object.extend("User"); 
    var userQuery = new Parse.Query(Parse.User);        
    var Late = Parse.Object.extend("Late");        
    var lateQuery = new Parse.Query(Late);
    userQuery.get("yCDDEWoiwM", {//this is where the userID goes
         success: function (userDetails) {
mLate = [];
mLate1 = [];

lateQuery.find({
     success: function (allLate) {
          var aLate = allLate
          for (var i = 0; i < aLate.length; i++){
          var oneLate = aLate[i];
          if(something valid) {
          mLate.push(oneLate)
          }else{
          mLate1.push(oneLate)

var relation = userDetails.relation("mLate");
               relation.add(mLate);
               userDetails.save();

推荐答案

您不能以这种方式扩展用户.所有特殊类(用户,安装,角色等)都应按以下方式处理:

You can't extend the user that way. All special classes (User, Installation, Roles, etc) should be addressed like this:

"_User", "_Roles", "_Installation"

例如:

var user = Parse.Object.extend("_User");

但是最好以这种方式引用它们:

But it's better to reference them in this way:

var user = Parse.Object.extend(Parse.User);

但是据我所知您也不需要创建user对象的第一行.如果您只想创建一个查询,那么您也不需要扩展"一个对象.通常,如果要创建指针,则使用扩展",但在这种情况下则不需要.

But as far as I can tell you also don't need the first line creating a user object. You also don't need to "extend" an object if all you wanted to do is created a query. Typically "extending" is used if you want to create a pointer but in your case you don't need to.

看看这对您是否有用:

//var user = Parse.Object.extend(Parse.User); 
var userQuery = new Parse.Query(Parse.User);        
//var Late = Parse.Object.extend("Late");        
var lateQuery = new Parse.Query("Late");
userQuery.get("yCDDEWoiwM", {//this is where the userID goes
     success: function (userDetails) {
           var relation = userDetails.relation("mLate");
           relation.add(mLate);
           userDetails.save(); 

如果您想使查询更具动态性,可以执行以下操作:

If you want to make the query a bit more dynamic you could do this:

var userQuery = new Parse.Query(Parse.User);               
queryQuery.equalTo("objectId", userId); 
userQuery.first().then(function(userDetails){

   var relation = userDetails.relation("mLate");
   relation.add(mLate);

   return Parse.Promise.when(userDetails.save());
}).then( // make your call to response.success ...);

另一个可能的问题是,您的函数在userDetails.save()操作有机会完成之前就返回了.如果您将查询修改为类似于上述内容,以使用 Promise ,它将确保对save()的异步调用实际上将完成.

Another possible issue is that your function is returning before your userDetails.save() operation has a chance to finish. If you modify your query to be something like the above to use a Promise then it will guarantee that the async call to save() will actually complete.

未显示的是诺言链的继续,以调用response.successerror,但是您可以添加它.

What is not shown is the continuation of the promise chain to call response.success or error but you can add that in.

这篇关于在解析中保存基本关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆