3

Good night everyone, I have the following problem. I'm using a sync function from WatermelonDB but it's giving this error when it runs. I'm sure an object manually to see why it doesn't work.

And that same object works if it's inserted out of sync. Does anyone know how to fix?

    await synchronize({
    database,
    pullChanges: async ({ lastPulledAt }) => {
      const { data } = await api.get(`/sync/pull/${1637416552 || 0}`);
      console.log(JSON.stringify(data.changes, null, 2));
      return {
        // changes: data.changes,
        changes: {
          sub_workspaces: {
            created: [
              {
                sub_workspace_id: 57,
                name: "Teiu",
                avatar_url: "-",
                slug: "teiu",
                workspace_id: 19,
              },
            ],
            updated: [],
            deleted: [],
          },
        },
        timestamp: 1637416552,
      };
    },
    pushChanges: async ({ changes }) => {},
  });
    import { tableSchema } from '@nozbe/watermelondb'
    
    const subWorskapceSchema = tableSchema({
        name: 'sub_workspaces',
        columns: [
            {
                name: 'sub_workspace_id',
                type: 'number',
            },  
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'avatar_url',
                type: 'string'
            },
            {
                name: 'slug',
                type: 'string'
            },
            {
                name: 'workspace_id',
                type: 'number'
            },
        ]
    })    
    export { subWorskapceSchema }

enter image description here

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
caiquegl
  • 31
  • 2

1 Answers1

0

Good morning, the error is because you are not receiving the 'id' field and watermelon requires it to create, update or delete a record. the structure of the changes is fine, it is only necessary to add the field 'id' with a value of type string try with:

await synchronize({
    database,
    pullChanges: async ({ lastPulledAt }) => {
      const { data } = await api.get(`/sync/pull/${1637416552 || 0}`);
      console.log(JSON.stringify(data.changes, null, 2));
      return {
        // changes: data.changes,
        changes: {
          sub_workspaces: {
            created: [
              {
                id: 'testid'
                sub_workspace_id: 57,
                name: "Teiu",
                avatar_url: "-",
                slug: "teiu",
                workspace_id: 19,
              },
            ],
            updated: [],
            deleted: [],
          },
        },
        timestamp: 1637416552,
      };
    },
    pushChanges: async ({ changes }) => {},
  });