I'm using LokiJS and it's fine for most parts. Except that I can't seem to delete a record from the database.
The find gets me the right record to delete, but neither remove() not findAndRemove() remove it after saveDatabase. What am I doing wrong?
(Note: I've tried saveDatabase outside the loadDatabase(...) callback - no change. whenever I re-run it, the record I thought I deleted is still there)
Here's the code: dbName is a valid path to the file
var db = new loki(dbName);
db.loadDatabase({}, function (e) {
var orders = db.getCollection("orders");
console.log(orders.count() + " orders found... iterating");
orders.find().forEach((v, i) => {
console.log(i + "." + v.key + " " + v.places.display + " " + v.theme + " " + moment(v.date).format("DD MMM YYYY HH:mm"));
});
}
let dKey = "some key that is valid";
if (!dKey) {
console.log("no key specified, returning");
return;
}
console.log("deleting specific record " + dKey);
let dObj = orders.find({ key: dKey });
if (!dObj || dObj.length == 0) {
console.log("no such record, returning");
return;
}
console.log(dObj[0]);
orders.remove(dObj[0]);
db.saveDatabase((e) => {
console.log("save complete");
});
}
});