Skip to content

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

Closed

Description

@omairvaiyani

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?

Activity

  1. gfosco commented on Mar 20, 2016

    @gfosco
    Contributor

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

  2. omairvaiyani commented on Mar 21, 2016

    @omairvaiyani
    ContributorAuthor

    @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.

  3. gfosco commented on Mar 21, 2016

    @gfosco
    Contributor

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

  4. omairvaiyani commented on Mar 21, 2016

    @omairvaiyani
    ContributorAuthor

    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.

  5. flovilmart commented on Apr 1, 2016

    @flovilmart
    Contributor

    what's that  .saveAsUser function?

  6. omairvaiyani commented on Apr 2, 2016

    @omairvaiyani
    ContributorAuthor

    Hi @flovilmart, it's a helper function:

    Parse.Object.prototype.saveAsUser =  function (user, changes)  {
           return this.save(changes || null, {sessionToken:user.getSessionToken()});
    };
    
  7. omairvaiyani commented on Apr 8, 2016

    @omairvaiyani
    ContributorAuthor

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

  8. borekwa commented on May 18, 2016

    @borekwa

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions