Parse-server: Parse relation schema mismatch when passing in array of objects

Created on 11 Mar 2016  路  16Comments  路  Source: parse-community/parse-server

Environment Setup

  • Parse Server 2.1.5
  • Localhost
  • Node 5.5
  • Mongo 3.1.9

    Steps to reproduce

It seems like the below code in my User's afterSave is causing a problem

feed.relation('forUsers').add(otherUsersInFamily);

otherUsersInFamily is just an array of Parse.User returned by a Parse.Query. When I tried a basic setup creating a Feed and adding forUsers (from a Parse.Query) it works. Its a wierd situation if you need more information please let me know

This particular line is causing an error

schema mismatch for Feed.forUsers; expected relation<_User> but got object

More code

userAfterSave: function(req) {
        var Feed = Parse.Object.extend('Feed');

        var user = req.object;
        var otherUsersInFamily = [];

        if (!user.get('family'))
            return console.log('user not in family', user.id);

        var query = new Parse.Query(Parse.User);
        query.equalTo('family', user.get('family'));
        query.notEqualTo('objectId', user.id);
        query.find()
            .then(function(users) {
                otherUsersInFamily = users;

                if (user.get('joinedFamily') !== true)
                    return;

                var promises = [];

                // add feed
                var feed = new Feed({
                    actor: user.toPointer(),
                    action: 'joined family',
                    title: user.get('family'),
                    subjectType: 'FamilyEvent',
                    subjectAction: 'joined'
                });
                console.log('user id', user.id, otherUsersInFamily)
                feed.relation('forUsers').add(otherUsersInFamily);     // <<< --- When this is commented things no error occur

                return feed.save();
            })
            .fail(function(err) {
                console.error(`User.afterSave Failed: ${user.id}, ${err.message}`);
                return Parse.Promise.error(err.message);
            });
}
troubleshooting

Most helpful comment

I also ran into this error but the reason is because I had created Collections in the db and, some time later, I changed the data structure of the Documents I was saving in that same Collection.

Have you changed the structure of any of the Documents since you began writing them to the db?

In my case, I had been using myParseObject.add() to add key/values, which actually performs operations on arrays. I realized later that I needed to use myParseObject.put() in order to simply have a key/value pair rather than an array. And that's when I started getting the error because I was saving to an existing Collection but with a different data structure than what is already saved in the db.

Here's the error I got:

com.parse.ParseRequest$ParseRequestException: schema mismatch for Profile.address; expected [object Object] but got String

To fix the problem:

I'm using a MongoDB and I deleted all Documents in the Collection named Profile. But that wasn't enough. I needed also to go into the _Schema Collection and delete any old definition of the Profile Collection there as well. Once I did that everything began working again.

To troubleshoot you should try changing the Class name of your ParseObject and see if it saves without error.

For example if you have: ParseObject object = new ParseObject("MyProfile")
then try changing it to: ParseObject object = new ParseObject("TestProfile")

If using a new class name for the ParseObject saves without error then you may have old Collections by the original name hanging around the db or in the _Schema. Clean them up and try again.

All 16 comments

Can you post the contents of the _User object from your _SCHEMA collection in mongo?

@drew-gross: thats the strange thing, I can't seem to access _* collections in mongo?

> db._SCHEMA.find().pretty()
2016-03-11T14:53:12.141+0800 E QUERY    [thread1] TypeError: db._SCHEMA is undefined :

Ok got it:

{
    "_id" : "_User",
    "username" : "string",
    "password" : "string",
    "email" : "string",
    "dateOfBirth" : "date",
    "family" : "string",
    "firstName" : "string",
    "lastName" : "string",
    "countryCode" : "string",
    "phoneNumber" : "string",
    "gender" : "string",
    "color" : "string",
    "timezone" : "number",
    "image" : "file"
}

For feeds:

{
    "_id" : "Feed",
    "actor" : "*_User",
    "action" : "string",
    "title" : "string",
    "subjectType" : "string",
    "subjectAction" : "string",
    "forUsers" : "relation<_User>"
}

Schema looks ok to me, forUsers should be a relation. The strange thing is AFAIK, I do not assign an object to the relation I always use

  • feed.relation('forUsers').add(Parse.User|Array<Parse.User>)

Your code looks correct. Does it also fail when you use only a single object? Or does it only fail when you try to save an array?

@drew-gross it fails when I save an array (actually I see the error but it appears the relation is saved fine). Its "OK" (no error) when I save an object:

Except that when I save an object, 1 by one (in a forEach loop), only the first one is saved

I'm investigating a bit further

Except that when I save an object, 1 by one (in a forEach loop), only the first one is saved

// objects is an array of 10 _User
objects.forEach((user) =>聽{
     owner.relation('toUsers').add(user);
});
return owner.save();

// request
POST /1/classes/Feed { 'user-agent': 'node-XMLHttpRequest, Parse/js1.7.1 (NodeJS 5.3.0)',
  accept: '*/*',
  'content-type': 'text/plain',
  host: 'localhost:8378',
  'content-length': '243',
  connection: 'close' } {
  "toUsers": {
    "__op": "AddRelation",
    "objects": [
      {
        "__type": "Pointer",
        "className": "_User",
        "objectId": "DsvTs15h5C"
      }
    ]
  }
}

This is a parse/node SDK error. cc: @andrewimm

@jiewmeng try saving the feed before adding objects to the relation

@jiewmeng do you still see the issue or it's fine for you? (besides the problem with the forEach, that seem to reside in the Parse JS SDK)

No response in many days, closing.

I have the same problem using Android SDK. "Schema mismatch, expected [Object object] but got Array."

ArrayList<ParseUser> guests = new ArrayList<ParseUser>();
guests.add(ParseUser.getCurrentUser());
eventObject.put("guests", guests);

// after this I call eventObject.saveInBackground()

@drew-gross could you confirm that my code is correct? I also tried with ParseObject instead of ParseUser, which also should work, right?

I get the same error when I use destroy!

I also ran into this error but the reason is because I had created Collections in the db and, some time later, I changed the data structure of the Documents I was saving in that same Collection.

Have you changed the structure of any of the Documents since you began writing them to the db?

In my case, I had been using myParseObject.add() to add key/values, which actually performs operations on arrays. I realized later that I needed to use myParseObject.put() in order to simply have a key/value pair rather than an array. And that's when I started getting the error because I was saving to an existing Collection but with a different data structure than what is already saved in the db.

Here's the error I got:

com.parse.ParseRequest$ParseRequestException: schema mismatch for Profile.address; expected [object Object] but got String

To fix the problem:

I'm using a MongoDB and I deleted all Documents in the Collection named Profile. But that wasn't enough. I needed also to go into the _Schema Collection and delete any old definition of the Profile Collection there as well. Once I did that everything began working again.

To troubleshoot you should try changing the Class name of your ParseObject and see if it saves without error.

For example if you have: ParseObject object = new ParseObject("MyProfile")
then try changing it to: ParseObject object = new ParseObject("TestProfile")

If using a new class name for the ParseObject saves without error then you may have old Collections by the original name hanging around the db or in the _Schema. Clean them up and try again.

Thanks, this solved my problem.

@fetching how did you delete the schema collection?

db._SCHEMA.remove({})
2019-04-16T11:50:07.349+0000 E QUERY [js] TypeError: db._SCHEMA is undefined : @(shell):1:1

above command never worked

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohmagdy picture mohmagdy  路  3Comments

darkprgrmmr picture darkprgrmmr  路  4Comments

carjo422 picture carjo422  路  3Comments

dcdspace picture dcdspace  路  3Comments

okaris picture okaris  路  4Comments