Skip to content

Called relation() on non-relation field even when field is relation. #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
omairvaiyani opened this issue Mar 20, 2016 · 8 comments · Fixed by #1424
Closed

Called relation() on non-relation field even when field is relation. #1112

omairvaiyani opened this issue Mar 20, 2016 · 8 comments · Fixed by #1424

Comments

@omairvaiyani
Copy link
Contributor

I've noticed this bug on several occasions since upgrading to 2.2.0. It tends to happen after the object has been saved.

Uncaught internal server error. [Error: Called relation() on non-relation field following] Error: Called relation() on non-relation field following
    at ParseUser.relation (/Users/~/Documents/github/~/node_modules/parse/lib/node/ParseObject.js:579:17)

Tried with Parse SDK 1.7.1 and 1.8.1. Anyone else experiencing this issue?

@gfosco
Copy link
Contributor

gfosco commented Mar 20, 2016

What is in the _SCHEMA collection record for this class & key?

@omairvaiyani
Copy link
Contributor Author

@gfosco

It's for _User

{
    "_id" : "_User",
   ....
    "followers" : "relation<_User>",
   ....
}

Calling user.relation('followers') works fine until the object is saved, after which it says that followers is a non-relation field.

@gfosco
Copy link
Contributor

gfosco commented Mar 21, 2016

Can you provide additional code... where are you calling it twice?.. Writing a failing test case would really be valuable here.

@omairvaiyani
Copy link
Contributor Author

Sure thing - The following is the simplest cloud function which fails:

/**
 * @CloudFunction Follow User
 *
 * Adds user to currentUser' 'following'
 * relation,
 * Sets Jobs to handle follow stats and
 * followed user's follower's relation.
 *
 * param {String} userIdToFollow
 */
module.exports.followUser = function (request, response) {
    var currentUser = request.user,
        userIdToFollow = request.params.userIdToFollow,
        userToFollow = new Parse.User();

    if (!currentUser && request.params.currentUserId) {
        currentUser = new Parse.User();
        currentUser.id = request.params.currentUserId;
    }

    if (!currentUser) {
        return response.error("You must be logged in.");
    }
    if (!userIdToFollow) {
        return response.error("Please send a userIdToFollow.");
    }

    userToFollow.id = userIdToFollow;


    currentUser.relation('following').add(userToFollow); // this works

    currentUser.saveAsUser(currentUser)
        .then(function () {

          currentUser.relation('following'); // This fails (it's no longer needed here as I've refactored the code, but the error is still an issue in other more complicated functions

            return Parse.Promise.when(
                UserEvent.newEvent('followed', userToFollow, currentUser),
                JobQueue.createJob(JobQueue.jobs.postFollowProcessing, {
                    user: currentUser,
                    userFollowed: userToFollow
                }, JobQueue.priorityLevel.high)
            );
        })
        .then(function (userEvent) {
            response.success({
                userEvent: userEvent
            });
        }, function (error) {
            response.error(error);
        });
};

I've just confirmed that the error subsides when I revert from 2.2.0 down to 2.1.6.

@flovilmart
Copy link
Contributor

what's that  .saveAsUser function?

@omairvaiyani
Copy link
Contributor Author

Hi @flovilmart, it's a helper function:

Parse.Object.prototype.saveAsUser =  function (user, changes)  {
       return this.save(changes || null, {sessionToken:user.getSessionToken()});
};

@omairvaiyani
Copy link
Contributor Author

Excellent, thank you! This was stopping me from upgrading past 2.1.6!

@borekwa
Copy link

borekwa commented May 18, 2016

Hey @flovilmart, I'm experiencing a similar issue to this one, as are some other users. See this issue. any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants