简体   繁体   English

lowdb:更新记录在索引N

[英]lowdb: update record at index N

I'm using lowdb ( https://github.com/typicode/lowdb ) with a persistent storage ( file-async ): 我正在使用具有永久性存储( file-async )的lowdbhttps://github.com/typicode/lowdb ):

const low = require('lowdb');
const db = low('db.json', {storage: require('lowdb/lib/storages/file-async')});
db.defaults({inputs: []}).value();

From a client I receive this object: 从客户端我收到此对象:

var data = {
  "idx": 1,
  "item": { "Name": "blabla", "Surname": "bleble" }
}

I want to update the record number idx with the new information received. 我想用收到的新信息更新记录号idx This doesn't update anything: 这不会更新任何内容:

if (data.idx < db.get("inputs").size()) {
    db.get("inputs").nth(data.idx)
        .assign(data.item)
        .write();
}

In my actual data I have more than 2 items. 在我的实际数据中,我有2个以上的项目。

Here the solution: 这里的解决方案:

db.get('inputs')
    .nth(data.idx)
    .assign(data.item)
    .value();
db.write();

Please note the last method should be a separate call. 请注意,最后一个方法应该是一个单独的调用。 Putting the write() call at the end of the chain doesn't work , at least in my setup. 至少在我的设置中,将write()调用放在链的末尾不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM