获取与PFObject云代码的关系

时间:2016-08-24 20:50:02

标签: javascript parse-platform pfobject

我的PFUserdays DayPFObject的关系。

在我的数据库中,它看起来像这样:

{
    "_id": "WjLAnMuRmp",
    "name": "c",
    "_created_at": {
        "$date": "2016-08-04T15:28:51.759Z"
    },
    "_updated_at": {
        "$date": "2016-08-24T19:44:44.774Z"
    },
    "days": [
        {
            "__type": "Pointer",
            "className": "Day",
            "objectId": "BrQwmKAbJC"
        },
        {
            "__type": "Pointer",
            "className": "Day",
            "objectId": "6wuDMl4kKI"
        }
    ]
}

非常直接。在我的云代码中,我尝试发送PFUser objectId,然后获取他们拥有的所有days并迭代它们。出于某种奇怪的原因,当我进行关系查询时,我一直得到0。

以下是我与之合作的内容:

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

  console.log("-getDayAveragesForUser");

  // Create the query on the User class
  var fetchedUser = new Parse.User({id:request.params.userObjectId});

  console.log("the fetched user: " + fetchedUser.id);

  var relation = fetchedUser.relation("days");

  var query = relation.query();
  query.limit(365);
  query.ascending("createdAt");
  query.find({
    success: function(results) {
      console.log("Successfully retrieved " + results.length + " Days.");
    },
    error: function(error) {
      console.log("Error: " + error.code + " " + error.message);
    }
  });
});

当我打印fetchedUser.id时,它是正确的,所以我知道我是正确的用户。这似乎基于文档示例:

var relation = user.relation("likes");
relation.query().find({
  success: function(list) {
    // list contains the posts that the current user likes.
  }
});

这应该可以正常工作。

===

我只是将其添加到我的Cloud Code中进行测试:

  var days = fetchedUser.get("days");

  console.log("type of: " + typeof days);

从此我得到:

type of: undefined

1 个答案:

答案 0 :(得分:1)

原因是你的日子不是作为关系而是作为指针保存。并且在解析关系和指针中以不同的方式处理。 要获取日期指针,您需要将查询更改为如下所示:

var userQuery = new Parse.Query(Parse.User);
userQuery.include("days"); // include the days pointer in the results
userQuery.get(request.params.userObjectId, {
  success: function(user) {
    // This function will *not* be called.
    console.log(user.get("days")); // print the days to console
  },
  error: function(error) {


  }
});