2016-07-14 55 views
0
  • 的Javascript
  • 解析云代码的阵列

在应用的过程中User可以申请加入另一个Users帐户。这样做的伪代码如下:追加项目以获取项目解析云代码

  1. 发上来,我们想加入
  2. 搜索,看是否存在于数据库中的username帐户的username
  3. 如果no回报
  4. 如果yes创建一个新的AccountRequest对象
  5. 将新创建的AccountRequest对象添加到我们正在搜索的用户。

我能够做到步骤1-4,但是我无法完成#5。

这是我正在使用的代码。

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

    console.log("-sendAccountAdditionRequest"); 

    // Create the query on the User class 
    var query = new Parse.Query(Parse.User); 

    // Set our parameters to search on 
    query.equalTo("username", request.params.adminUsername); 

    // Perform search 
    query.find({ 

    // We found a matching user 
    success: function(results) { 

     Parse.Cloud.useMasterKey(); 

     var fetchedUser = results[0] 

     console.log("--found user"); 
     console.log("--creating new AccountRequest"); 

     // Create a new instance of AccountRequest 
     var AccountRequestClass = Parse.Object.extend("AccountRequest"); 
     var accountRequest = new AccountRequestClass(); 

     // Set the User it is related to. Our User class has a 1..n relationship to AccountRequest 
     accountRequest.set("user", fetchedUser); 

     // Set out other values for the AccountRequest 
     accountRequest.set("phone", request.params.adminUsername); 
     accountRequest.set("dateSent", Date.now()); 

     // Save the new AccountRequest 
     accountRequest.save(null,{ 

      // Once the new AccountRequest has been saved we need to add it to our fetched User 
      success:function(savedRequest) { 

       console.log("---adding AccountRequest to fetched User"); 

       // 
       // === This is where stuff breaks 
       // 

       var requestRelation = fetchedUser.relation("accountRequest"); 

       // Now we need to add the new AccountRequest to the fetched User. The accountRequest property for a User is array...I'm not sure how I'm suppose to append a new item to that. I think I need to somehow cast results[0] to a User object? Maybe? 
       requestRelation.add(savedRequest); 

       // We perform a save on the User now that the accountRequest has been added. 
       fetchedUser.save(null, { 

        success:function(response) { 
         console.log("----AccountRequest complete"); 
         response.success("A request has been sent!"); 
        }, 

        error:function(error) { 
         // This is printing out: ParseUser { _objCount: 2, className: '_User', id: 'QjhQxWCFWs' } 
         console.log(error); 
         response.error(error); 
        } 
       }); 


       // 
       // ================================ 
       // 


       //response.success("A request has been sent!"); 
      }, 

      // There was an error saving the new AccountRequest 
      error:function(error) { 
       response.error(error); 
      } 
     }); 
    }, 

    // We were not able to find an account with the supplied username 
    error: function() { 
     response.error("An account with that number does not exist. Please tell your administrator to sign up before you are added."); 
    } 
    }); 
}); 

我相信我的问题是获取从初始查询返回的搜索结果中的accountRequest关系。另外需要注意的是,我还没有创建PFUseraccountRequest属性,我的理解是,当我执行save函数时,这将自动由Parse完成。

+0

您是使用托管Parse.com服务还是开源解析服务器? –

+0

@MichaelHelvey开源 – random

+1

只是想知道,因为(虽然不完全是问题的主题)只是想指出,在节点环境(parse-server)'Parse.Cloud.useMasterKey();'不会做任何事情。如果你想使用你的masterKey,你必须在'save'或'query'的'options'参数中明确地使用它。即'object.save(null,{useMasterKey:true});' –

回答

1

是否为您创建accountRequest取决于是否将类级别权限设置为允许客户端应用程序添加字段。对于您的Parse.User,这可能设置为NO。

但无论如何,Parse.User上的关系并不需要,因为您已经在AccountRequest类中建立了它。给定一个用户,你可以得到它的accountRequests有:

PFQuery *query = [PFQuery queryWithClassName:@"AccountRequest"]; 
[query whereKey:@"user" equalTo:aUser]; 
query.find()... 

这相当于获得的用户的关系,获得其查询并运行它。

关于你的代码的一些注意事项:(a)findOne将为你节省一条线,当你知道只有一个结果时,(b)使用Parse.Promise会真正整理一些东西。

+0

真棒男人我很感激它。对于Parse的CloudCode方面来说很新,所以试图通过它的方式工作。 :) – random

+0

我试图找到'AccountRequest'购买查询'user'属性的建议,但它从未返回任何结果。我向我的'AccountRequest'类中添加了一个'ownerObjectId'属性,并且我能够成功地对此进行查询。我在这里发布了一个关于'user'关系查询的新问题,如果您有任何建议:http://stackoverflow.com/questions/38484995/query-relation-in-parse – random

+1

当然。将在大约一个小时内检查它。不应该需要任何新的属性,但我会检查出来 – danh