获取与 PFObject 云代码的关系
Fetching relation to PFObject cloud code
我有一个 PFUser
与 Day
PFObject
.
有 days
关系
在我的数据库中它看起来像这样:
{
"_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.
}
});
这应该可以正常工作。
===
我只是将其添加到我的云代码中进行测试:
var days = fetchedUser.get("days");
console.log("type of: " + typeof days);
由此我得到:
type of: undefined
原因是你的日子不是保存为关系,而是保存为指针。在解析中,关系和指针以不同的方式处理。
为了获取日期指针,您需要将查询更改为如下所示:
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) {
}
});
我有一个 PFUser
与 Day
PFObject
.
days
关系
在我的数据库中它看起来像这样:
{
"_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.
}
});
这应该可以正常工作。
===
我只是将其添加到我的云代码中进行测试:
var days = fetchedUser.get("days");
console.log("type of: " + typeof days);
由此我得到:
type of: undefined
原因是你的日子不是保存为关系,而是保存为指针。在解析中,关系和指针以不同的方式处理。 为了获取日期指针,您需要将查询更改为如下所示:
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) {
}
});