1
votes
BlogApp.Collections.Blogs = Parse.Collection.extend({
    model: BlogApp.Models.Blog,
    query: (new Parse.Query(BlogApp.Models.Blog)).equalTo("author", "xMQR0A1Us6").descending('createdAt').limit(9)
});

The above doesn't seem to work. I can do all sorts of things with columns that already exist in the class, such as .equalTo("productType", "SHIRT"), for example, but I can't link to the author which exists in a separate class User.

How can I restrict the query to retrieve only items from "author" (a pointer) equal to an objectId which exists in the User class?

Model:

BlogApp.Models.Blog = Parse.Object.extend('MarketDesign', {

    update: function(form) {

        if ( !this.get('ACL') ) {
            var blogACL = new Parse.ACL(Parse.User.current());
            blogACL.setPublicReadAccess(true);
            this.setACL(blogACL);
        }

        BlogApp.category.id = form.category;

        this.set({
            'title': form.title,
            'url': form.title.toLowerCase()
            .replace(/[^\w ]+/g,'')
            .replace(/ +/g,'-'),
            'category': BlogApp.category,
            'comment': form.content,
            'author': this.get('author') || Parse.User.current(),
            'authorName': this.get('authorName') || Parse.User.current().get('username'),
            'time': this.get('time') || new Date().toDateString()
        }).save(null, {
            success: function(blog) {
                Parse.history.navigate('#/admin', { trigger: true });
                window.location.reload();
            },
            error: function(blog, error) {
                console.log(error);
            }
        });
    }

});
2

2 Answers

2
votes

There's a distinction between objectId -- which is just a string -- and a pointer. To equate a Pointer column in a query, you must pass a parse object. So, for example, to find Blogs where author is the current user...

var user = Parse.User.current();   // no .id, that's important!
BlogApp.Collections.Blogs = Parse.Collection.extend({
    model: BlogApp.Models.Blog,
    query: (new Parse.Query(BlogApp.Models.Blog)).equalTo("author", user).descending('createdAt').limit(9)
});

If you have only the objectId, then build an object with it, like this:

var user = Parse.User.createWithoutData("xMQR0A1Us6"); 

But I don't recommend this. If you have an object id, you must have once had the whole object to which it belongs. In general, make it a practice to not retain object ids; instead, keep the objects they belong to so that you can use any part of them later.

0
votes

I did some research and found that you can't use the .equalTo method to query an element in the pointed-to class: https://coderwall.com/p/-uzbrq/multi-dimensional-querying-parse-com-javascript-sdk

This isn't the answer because it doesn't solve my problem, but hopefully somebody else will see this and have a better picture of what I'm trying to accomplish:

BlogApp.Collections.UserDesigns = Parse.Collection.extend({
    model: BlogApp.Models.Blog,
    query: (new Parse.Query(BlogApp.Models.Blog)).matchesQuery("author", Parse.User.current().id).descending('createdAt').limit(3)
});