antoniopresto / react-native-local-mongodb

react-native local mongodb storage.
MIT License
304 stars 66 forks source link

Remove or insert requires application restart to see their effects #40

Closed mg4u closed 6 years ago

mg4u commented 6 years ago

Hi all, Here is my code

var Datastore = require('react-native-local-mongodb')
let db = new Datastore({filename: 'tasks', autoload: true})
db.insert({task_id:1,name:test});

When I navigate from the current scrren to another screen and write this code

var Datastore = require('react-native-local-mongodb')
let db = new Datastore({filename: 'tasks', autoload: true})
await db.find({task_id:{$exists:true}}, async function (err, tasks) {//all data
    console.log('db tasks',tasks)
});

No tasks appear, But if I restart the application the tasks shown in the app.

Same thing for remove. I remove everything on log out, if I make login again right back the old tasks still available on the application, it disappears if only I restart the app.

The Versions

"react": "16.0.0",
 "react-native": "0.51.0",
"react-native-local-mongodb": "^2.2.2",

What can I do to solve this issue?

antoniopresto commented 6 years ago

You need to use the same db instance between the screens.

antoniopresto commented 6 years ago

Please. Feel free to reopen.

mg4u commented 6 years ago

@antoniopresto Thanks a lot, it works now. In case someone faces this issue like me, there are 2 solutions: 1- Use the same instance by creating it in some global.js of config.js file and import it to any component file you want to use. 2- Instead of autoloading the DB just create the object and put any find, remove, update or insert inside loadDatabse block