从Parse中的第二级指针获取数据

Get data from 2nd level pointer in Parse

本文关键字:二级 指针 获取 数据 Parse      更新时间:2023-09-26

我设置了三个相关类:_User、Article和Profile。在Article中,我有一个名为author的指针用于_User,在Profile中,我也有相同的指针;指向_User的指针,但命名为user

现在,我想从Article中检索数据,Profile中的cols firstnamelastname,其中Article中的指针与_User中的objectId匹配,以及Profile中的指针。

基本上是我在SQL中解决的内部连接。

我如何用一个解析调用来处理这个?

这是我目前为止写的:

var Article = Parse.Object.extend("Article");
var query = new Parse.Query(Article);
query.include("category");
query.find({
  success: function(results) {
    console.log("Successfully retrieved " + results.length + " article(s):");
    // Do something with the returned Parse.Object values
    for (var i = 0; i < results.length; i++) {
      var object = results[i];
      console.log(object.get('title'));
      console.log(object.get('content'));
      console.log(object.get('category').get("categoryName"));
    }
  },
  error: function(error) {
    console.log("Error: " + error.code + " " + error.message);
  }
});

在OP不厌大烦地对数据和期望结果进行完整(最小)描述的情况下,回答这些问题是很愉快的。

我想我理解你想要获取文章,而对于每一个你想要获取概要文件,并且概要文件和文章(逻辑上)通过一个指向User的公共指针连接在一起。

这可以通过每篇文章附加一个查询来完成。为了清晰和可维护性,我喜欢把这些东西分解成简短的、逻辑的承诺返回函数,所以…

// for handy array functions, like _.map, and turning var args into simple arrays
var _ = require('underscore');
// note: include underscore this way in cloud code (nodejs)
// for browser, see underscorejs.org to add to your project
// this will answer a promise that is fulfilled with an array of the form:
// [ { article:article_object, profile:profile_object }, {...}, ...]
function articlesWithProfiles() {
    var query = new Parse.Query("Article");
    query.include("category");
    query.include("author");
    return query.find().then(function(articles) {
        var promises = _.map(articles, function(article) {
            return profileForArticle(article);
        });
        return Parse.Promise.when(promises);
    });
}
// return a promise that's fulfilled by associating the given article with it's profile
function profileForArticle(article) {   
    var author = article.get("author");
    var query = new Parse.Query("Profile");
    query.equalTo("user", author);
    return query.first().then(function(profile) {
        return { article:article, profile:profile };
    });
}
// call it like this
articlesWithProfiles().then(function() {
    // see edit below
    var result = _.toArray(arguments);
    console.log(JSON.stringify(result));
}, function(error) {
    // handle error
    console.log(JSON.stringify(error));
});