Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

Not allowed to change record outside of create/update() #1834

Closed gaetan-hexadog closed 2 weeks ago

gaetan-hexadog commented 2 weeks ago

Hi,

How can I create or update my model ? I always get the following error message Not allowed to change record outside of create/update() with a simple code like

const usersCollection = database.get<User>('users');
return await database.write(async () => {
        // Création d'un nouvel utilisateur
        const user = await usersCollection.create(newUser => {
                newUser.email = userData.email;
                newUser.name = userData.name || '';
                newUser.createdAt = new Date(userData.created_at);
                newUser.updatedAt = new Date();
        });

        return user;
});

regards

KrisLau commented 2 weeks ago

.create should be chained to the end of the get: https://watermelondb.dev/docs/CRUD#create-a-new-record

gaetan-hexadog commented 2 weeks ago

Thanks but unfortunately I still get the same issue with the following code

return await database.write(async () => {
     // Création d'un nouvel utilisateur
     return await database.get('users').create(newUser => {
          newUser.email = userData.email;
          newUser.name = userData.name;
     });
});
gaetan-hexadog commented 2 weeks ago

Apparently there was a problem with the global configuration of my project. I've restarted a project from scratch and everything works. thanks