Suji
Suji

Reputation: 769

How to properly use Parse.query.equalTo?

In my web application when the user logs in they should see all the documents they made. However when I test my application, I see all the documents including the ones that I didn't make. I believe this is because I am not using Parse.query.equalTo correctly.

Here is my code:

    router.post('/login', function (req, res) {
    var tempname = req.body.username;
    var username = tempname.toLowerCase();
    var password = req.body.password;
    var login = Promise.promisify(Parse.User.logIn);
    var promerror = Promise.reject("error");
    var Files = Parse.Object.extend("File");
    var query = new Parse.Query(Files);
    query.include('user');
    var finds = query.find;
    var doc = {};
    var name, url;
    return login(username, password).catch(function (error) {
    }).then(function (user, error) {
        query.equalTo("user", Parse.User.current());
        console.log(JSON.stringify(Parse.User.current()));
        if (user) {
            res.render('logins', { Message: "Username and/or password don't match" });
        }
        var temp;
    }).catch(function (err) {
        res.render('logins');
    }).then(query.find(function (results) {
        for (var i = 0; i < results.length; i++) {
            var object = results[i];
            var userID = object.get('user');
            console.log("current user");
            var codefile = object.get('javaFile');
            temp = codefile.name();
            name = temp.split("-").pop();
            url = codefile.url();
            doc[name] = url;
        }
    })).catch(function (error) {
        promerror(error);
    }).finally(function () {
        res.render('filename', { title: 'File Name', FIles: JSON.stringify(doc) });
    }).done();
});

When the user logs in, it should go to the database retrieve all the documents the user made, and save it to doc, the doc is then send to the client side to be displayed.

here is the code for user saving a file:

 newFile.save({ user: Parse.User.current(), fileName: newnames, javaFile: parseFile })

Upvotes: 0

Views: 298

Answers (1)

papergodzilla
papergodzilla

Reputation: 161

I'm not sure, but try setting the File user field to the object id instead of the user object itself. Parse objects sometimes will return a pointer, sometimes the id, etc. So you might be getting unexpected behavior where you're setting the field to Parse.User.current(). Besides that, you look like you're using equalTo correctly.

Otherwise, basically the only way to debug cloud code is to console.log and view the logs.

As a side note, you should be setting the ACL of each File before you save it... that way, a user wouldn't be able to view another user's files anyway (unless these files are meant to be public).

Upvotes: 1

Related Questions