从指针字段中检索信息

时间:2016-01-12 01:21:17

标签: javascript parse-platform

我尝试按照此post的建议从Pointer检索字段,但我总是undefined

这是我的表Publication的样子:

  • userId - > User
  • subCategoryId - > SubCategory
  • title
  • description

我的SubCategory表:

  • categoryId - > Category
  • name
  • isActive

这是我的尝试(桌子上只有一排):

var user = Parse.User.current();
var User = Parse.Object.extend("User");
var userQuery = new Parse.Query(User);
userQuery.equalTo("objectId", user.id);

var Publication = Parse.Object.extend("Publication");
var publicationQuery = new Parse.Query(Publication);
publicationQuery.include("subCategoryId");
publicationQuery.matchesQuery("userId", userQuery);        
publicationQuery.find({
    success: function(publications) {
        console.log(publications[0].get("title"));
        // This one returns undefined
        console.log(publications[0].get("subCategoryId"));
    }, error: function(error) {
        // Nothing here as suggested by @adolfosrs
        console.log(error);
    }
});

我需要的是:

publications[0].get("subCategoryId").get("name");

但显然后者抛出:

Uncaught TypeError: Cannot read property 'get' of undefined

1 个答案:

答案 0 :(得分:2)

如果您的解析数据库中有指针,如下所示,则无需使用objectIds。

enter image description here

您应该拥有的数据是这样的:

公开:

  • 用户(Pointer <_User>
  • subCategory(Pointer <SubCategory>
  • title(String
  • description(String

子类别:

  • 类别(Pointer <Category>
  • 名称(String
  • isActive(boolean

因此,如果您按预期保存数据,则必须执行以下操作:

var currentUser = Parse.User.current();
var Publication = Parse.Object.extend("Publication");
var publicationQuery = new Parse.Query(Publication);
publicationQuery.equalTo("user", currentUser); 
publicationQuery.include("subCategory");       
publicationQuery.find({
    success: function(publications) {
        console.log(publications[0].get("title"));
        // This one returns undefined
        console.log(publications[0].get("subCategory").get("name");
    }
});