Nozbe / WatermelonDB

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

I am unable to update a record #982

Open santanna106 opened 3 years ago

santanna106 commented 3 years ago

I am testing the use of watermelonDb in React-native.

I created a simple code and was able to insert and delete a record. However, I can't update. When I look at the record, the change was not performed

Code Update

async upTest(){
    const database = await getDataBase();
    const usersCollection = await database.get('users')
     await database.action(async () => {
      const usr = await usersCollection.find('u4m31hub33v9128q')
      await usr.update(usr => {
        usr.name = 'TTTT'  
      })

    })

   const usr2 = await usersCollection.find('u4m31hub33v9128q')
   console.log('Not Changed: ',usr2);
  }
alanmatiasdev commented 3 years ago

What error are you getting?

santanna106 commented 3 years ago

It has no error. But the data is not updated.

alanmatiasdev commented 3 years ago

Weird. For me the code seems to be correct. Are you managing to return the values in the usersCollection? What version of WatermelonDB?

santanna106 commented 3 years ago

I am able to insert the record and return the records. I just can't update.

I am using watermelondb ("^0.20.0")

my package.json :

{
  "name": "AppAcompanhaEntrega",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@nozbe/watermelondb": "^0.20.0",
    "@nozbe/with-observables": "^1.0.8",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/picker": "^1.8.1",
    "@react-navigation/bottom-tabs": "^5.11.8",
    "@react-navigation/drawer": "^5.11.4",
    "@react-navigation/native": "^5.8.10",
    "@react-navigation/stack": "^5.12.8",
    "axios": "^0.21.1",
    "react": "16.13.1",
    "react-native": "0.63.4",
    "react-native-awesome-alerts": "^1.4.2",
    "react-native-camera": "^3.42.2",
    "react-native-geolocation-service": "^5.1.1",
    "react-native-gesture-handler": "^1.9.0",
    "react-native-maps": "0.27.1",
    "react-native-maps-directions": "^1.8.0",
    "react-native-masked-text": "^1.13.0",
    "react-native-navigation-bar-color": "^2.0.1",
    "react-native-reanimated": "^1.13.2",
    "react-native-safe-area-context": "^3.1.9",
    "react-native-screens": "^2.15.0",
    "react-native-uuid": "^2.0.0",
    "react-native-vector-icons": "^7.1.0",
    "realm": "^10.3.0",
    "styled-components": "^5.2.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/plugin-proposal-decorators": "^7.12.12",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^26.6.3",
    "eslint": "^7.14.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "^0.64.0",
    "react-test-renderer": "16.13.1"
  },
  "jest": {
    "preset": "react-native"
  }
}
0x7An commented 3 years ago

@santanna106 can you wrap your code with a try/catch, and log the error?

I'm having a similar problem where my model has an undefined update function.

const updateTask = async (taskId: number, ownerId: number) => {
  return await database.action(async () => {
    try {
      const tasksCol = database.collections.get('tasks');

      let targetTask = await tasksCol.query(Q.where("exid", taskId)).fetch();

      // even with just one record, fetch returns a list
      return await targetTask[0].update((task) => {
        task.tecnhician_id = technicianId;
      });
    } catch (e) {
      console.log('❌', e);
    }
  });
};
santanna106 commented 3 years ago

@andersondsl I needed to finish the project and I changed the watermelondb for realmdb. I will create a test project to test your suggestion

0x7An commented 3 years ago

@santanna106 Nevermind, the code i've posted above works correctly,.

Inside a database action, you will always need a try/catch.

oh3vci commented 3 years ago

@santanna106 Did you have 'updated_at' column in the 'users' schema? I solved the same problem by adding the 'updated_at' column to the schema.

stale[bot] commented 2 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

abdulragib commented 1 month ago

try { await database.write(async () => { const individualChat = database.collections.get('individualChat'); const existingIndividualChat = await individualChat .query(Q.where('id', id)) .fetch(); console.log('existing', existingIndividualChat[0]); await existingIndividualChat[0].update(individualItem => { console.log('field', individualItem._raw.deleted); individualItem._raw[field] = value || ''; individualItem._raw.updatedAt = new Date(); }); }); } catch (err) { console.log( 'Error in updating field in IndividualChat in WatermelonDB: ', err, ); }

abdulragib commented 1 month ago

@santanna106 Did you have 'updated_at' column in the 'users' schema? I solved the same problem by adding the 'updated_at' column to the schema.

Same here , snake we have to follow in watermelondb , those who use camelcase on remote db will face issue