Trying to dynamically add entries of people and fruit that they own in Loki JS but I keep getting the Trying to update unsynced document. Please save the document first by using insert() or addMany()
error. Anyone have any ideas of how to do this successfully?
I've logged the values coming in and they match what is found in the db already.
const loki = require('lokijs');
const db = new loki('loki.json');
const peopleAndFruits = db.addCollection('peopleAndFruits');
peopleAndFruits.insert({ dbname: 'person', fruits: 10 });
const createOrInsertUserToDb = (name, fruitNumber) => {
const data = peopleAndFruits.find({ dbname: name });
console.log('db entry before updated ', data[0].dbname);
if (data[0].dbname !== name) {
// console.log('this person did not exist in our database');
peopleAndFruits.insert({ dbname: name, fruits: fruitNumber });
} else {
// console.log('previously existing entry');
peopleAndFruits.update({
dbname: data[0].dbname,
fruits: Number(fruitNumber + data.fruits),
});
}
// console.log('data after update or insert', peopleAndFruits.data);
db.saveDatabase();
};