1
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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

13 Comments

Would there be any general reasons as to why this line: var user = Parse.User.createWithoutData("xMQR0A1Us6"); just flat out kills my app?
That's how to make a user from an id. Surprised that it causes a crash
I get TypeError: Parse.user is undefined in my console.
User has an uppercase U.
Does that actually matter given that user is just a variable we've defined?
|
0

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

5 Comments

Are you trying to find Blogs where the blog author is the current user? That's easy (and described in the first part of my answer). The other part of my answer dealt with the id xMQR0A1Us6 in the OP. What user does that id belong to and where did you get it? I've edited my answer to clarify. To query for author == current user, the code you've put in this answer is sooo close. The point is, we pass objects to compare with pointer fields.
Also, the post you site deals with a more complex problem than you're trying to solve here. What you want to do is common and easy, once you get the hang of how to handle pointers.
The Id starting with X is just the object Id of a random User. I was testing different queries. The query is going to be used for the user's profile, displaying all their posts. So at least when viewing your own, you will want the current User's objectId.
Try current user for starters with the code in my (edited) answer. That should show you that it works (if you have blogs by the current user). Then we'll deal with random users next.
Thank you very much! Yes, that worked perfectly. What I didn't realize is that I can't just query the "string" that appears in the column, because the value that appears is not a string, per se, but an identifier connecting the classes. Cheers.

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.