【问题标题】:Append item to array of fetched item Parse Cloud Code将项目附加到获取的项目数组 Parse Cloud Code
【发布时间】:2016-07-14 19:17:13
【问题描述】:
  • Javascript
  • 解析云代码

在我的应用程序中,User 可以请求加入另一个 Users 帐户。其伪代码如下:

  1. 发送我们要加入的帐户的username
  2. 搜索数据库中是否存在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 会自动完成。

【问题讨论】:

  • 您使用的是托管的 Parse.com 服务,还是开源的 parse-server?
  • @MichaelHelvey 开源
  • 只是想知道,因为(虽然不完全是问题的主题)只是想指出在节点环境(解析服务器)Parse.Cloud.useMasterKey(); 不会做任何事情。如果你想使用你的masterKey,你必须在savequeryoptions 参数中显式使用它。 IE。 object.save(null, {useMasterKey: true});
  • @MichaelHelvey 明白了。非常感谢您的反馈!

标签: javascript parse-platform parse-cloud-code pfuser


【解决方案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 会真正整理一下。

【讨论】:

  • 很棒的人,我很感激。 Parse 的 CloudCode 方面很新,所以我试图通过它。 :)
  • 我尝试了您的建议以查找 AccountRequest 购买查询 user 属性,但它从未返回任何结果。我在我的AccountRequest 类中添加了一个ownerObjectId 属性,并且我能够成功地对其进行查询。如果您有任何建议,我在user 关系查询上发布了一个新问题:stackoverflow.com/questions/38484995/query-relation-in-parse
  • 当然。大约一个小时后会检查。应该不需要任何新属性,但我会检查一下
猜你喜欢
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 2016-01-23
  • 2020-03-09
  • 1970-01-01
  • 2013-12-03
  • 2014-02-20
  • 2017-09-28
相关资源
最近更新 更多