1

I have a setup with Three relevant classes: _User, Article, and Profile. In Article I have a pointer named author for _User, and in Profile, I have the same; a pointer, but named user, for _User.

Now, I want to retrieve data from Article, with the cols firstname and lastname in Profile, where the pointer in Article matches the objectId in _User, and the pointer in Profile.

Basically what I would solve with an inner join in SQL.

How do I go about this with just one parse call?

This is what I have so far:

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);
  }
});

1 Answer 1

2

Its a pleasure answering questions where the OP took the trouble to include a complete (and minimal) description of the data and the desired result.

I think I understand that you want to get Articles, and for each you want to get Profiles, and that the Profiles and articles are (logically) joined via a common pointer to User.

This can be done using an additional query per article. For clarity and maintainability, I like to break these things up into short, logical promise-returning functions, so...

// 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));
});
Sign up to request clarification or add additional context in comments.

10 Comments

Hmmm... Your snippet makes sense to me, but I can't seem to make it work - and it's something else that I am not qualified to guess. I've included underscore.js in my HTML, but I get this error: Uncaught ReferenceError: require is not defined EDIT: Oh wait, this is node.js? I'm not writing in node.js... How would I do _.map and _.toArray without underscore.js?
Require is how you use it in nodejs. To use it in your web app, get the source from (underscorejs.org which will lead you to github.com/jashkenas/underscore) and include it in your app with a <script> tag.
Yup, that's what I did as well, and included the underscore.js file in my HTML. But now I get this: Uncaught TypeError: Cannot read property 'then' of undefined <-- which is on the line after //call it like this
Well that's just a convenience library. Should I add code to do those couple of functions natively?
If it isn't too much trouble, I would sorely appreciate it :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.