Schema mismatch, expected Pointer

I’m getting the following error:

error: Parse error: schema mismatch for Registration.userPointer; expected Pointer<_User> but got Object

Any idea of what might be causing this? It happens on a .save() for a new object. userPointer is created as schema.addPointer('userPointer', '_User');, and the object passed in .set() is the created with const user = new Parse.User(); ...; await user.signup(null, { useMasterKey: true });

The code is running on a jest test, and similar code is running properly in other parts of the code without errors.

Would you mind to share the lines of code that are throwing this error?

Sure, it’s more or less this. In fact, there’s another weird behavior that might be linked to the problem that I’ll go over below:

async function createSchema() {
  // creates user and an Event object 

  const schemaRegistration = new Parse.Schema('Registration');
  schemaRegistration.addPointer('userPointer', '_User');
  schemaRegistration.addPointer('eventPointer', 'Event', { required: true });
  await schemaRegistration.save();
 });

async function makeUser (): Promise<Parse.User> {
  const randomEmail = faker.internet.email();
  const user = new Parse.User();
  user.set('name', faker.name.findName());
  user.set('email', randomEmail);  
  // other fields
  return await user.signUp(null, { useMasterKey: true });
}

async function makeEvent (): Promise<Event> {
  const event = new Event();
  // some event.set() here
  await event.save(null, { useMasterKey: true });
  return event;
}

async function makeRegistration (user: Parse.User, event: FanfestEvent): Promise<Registration> {
  const registration = new Registration();
  registration.set('userPointer', user);
  registration.set('eventPointer', event);
  await registration.save(null, { useMasterKey: true });
  return registration;
}

describe('notifications', () => {
  it('test that breaks', async () => {
    // several events are created here with different data
    const event = makeEvent();

    // test breaks if this is not run again here, see below
    const serverURL = 'http://localhost:1337/parse';
    Parse.initialize(process.env.APP_ID);
    Parse.CoreManager.set('SERVER_URL', serverURL);
    Parse.CoreManager.set('MASTER_KEY', process.env.MASTER_KEY);

    const user = await makeUser();

    const registration = await makeRegistration(user, event);
    expect(registration).toBeTruthy();
  );
});

If that Parse.initialize() code isn’t run again in the middle of the test, I get a Cannot use the Master Key, it has not been provided. This is specific to when the user is created, all other events (4 instances are created) work perfectly well with useMasterKey. If I comment the event code and run only the user creation I still get the same error. And yes, it has been properly initialized before.

console.log(user) prints something like ParseUser { _objCount: 0, className: '_User', id: 'xoEs1IYVCZ' }. That _objCount seems weird, and the user object is not persisted on the database. I’m guessing this is related to the masterKey issue above, but I’m at a complete loss on why this is happening.

Pretty much the exact same code runs perfectly well on a seeder script, which make me wonder if somehow Jest is getting in the way. I tried Parse.Object.disableSingleInstance(); on Jest’s initialization but there was no difference.

Instead of:

return await user.signUp(null, { useMasterKey: true });

try:

await user.save(null, { useMasterKey: true });
return user;

Talking about jest, I’d need to better understand your setup. It might be related to the fact that you are probably running a Parse Server instance in the same process. Parse Server also instantiates the SDK. You can find some inspiration here: parse-blockchain/packages/integration at master · parse-community/parse-blockchain · GitHub

I had tried save() instead, but there’s no difference.

Regarding Jest, I had issues running the server in the same process, so I’m not doing that. This is what I’m doing to set things up:

beforeAll(async () => {
  Parse.initialize(process.env.APP_ID);
  Parse.CoreManager.set('SERVER_URL', serverURL);
  Parse.CoreManager.set('MASTER_KEY', process.env.MASTER_KEY);
  Parse.Object.disableSingleInstance();
});

afterEach(async () => {
  await Parse.User.logOut();
  Parse.Storage._clear();
});

The test fails even if it’s the only one running, so it’s not a side effect from a different test.

But I solved the issue with these changes:

  • removed a lingering const ParseServer = require('parse-server').default; that was still there from when I was trying to run the server as part of the test, even though it was not being used anymore.
  • the code started to complain about Parse being undefined in random files. It was supposed to be a global and the main code runs properly. I added const Parse = require('parse/node'); on those files – any idea of why I needed to do that?

I also still don’t get why this affected just one of many tests.