如何将参数传递给neDB find-Query?

时间:2019-03-28 16:44:47

标签: javascript node.js electron nedb

我想在Electron应用程序中使用neDB构造一个非常简单的find-Query。在此应用程序中,用户可以添加新的费用,包括名称,值,间隔,类型和类别。 用户还可以在运行时添加类别,这些类别将保存到名为“ categories.db”的文件中,而其他所有内容都将保存在“ finances.db”中

示例:

categories.db

{"name":"Salary","_id":"HkRc1Y7BIWrSi1jr"}
{"name":"Sports","_id":"MSAqT2zQ9Am55SrI"}
{"name":"Utilities","_id":"OAQbd3A9JLAwRYnx"}
{"name":"Rent","_id":"iGEjAMTimweFqb4b"}
{"name":"Other","_id":"wW3v3JtpXxNgxCMZ"}

finances.db

{"name":"blubOne","value":"16","interval":"30","type":"Expense","category":"Sports","_id":"IpCwOenE1shUQioX"}
{"name":"bla","value":"420","interval":"30","type":"Income","category":"Salary","_id":"MhtmbJs7fOgzToUJ"}
{"name":"Dropbox","value":"9.99","interval":"30","type":"Expense","category":"Utilities","_id":"UzgCJjR7qXaxr3QJ"}
{"name":"mobile","value":"196","interval":"30","type":"Income","category":"Other","_id":"a9lJE22zLi0eiCBp"}
{"name":"electricity","value":"50","interval":"30","type":"Expense","category":"Utilities","_id":"z8dsjGDMg32s4MrO"}

我已经可以执行此查找查询,如下所示:

database.js

exports.filterByCategory = function(fnc){
    db.finances.find({category: "Sports"}, function(err, docs){
        fnc(docs)
    })
}

我以这种方式执行此功能:

summary.js

database.filterByCategory(function(filteredCategory){
    if (filteredCategory!== 0){
        for (i = 0; i < filteredCategory.length; i++){
                 console.log("FILTER: " + filteredCategory[i].name)
            })
        }
    }

但是我很难将参数传递给搜索查询(类似这样):

database.js

exports.filterByCategory = function(fnc, arg){
    db.finances.find({category: )}, function(err, docs){
        fnc(docs)
    })
}

我在neDB文档中也没有发现与此问题有关的任何东西。

https://github.com/louischatriot/nedb/blob/master/README.md


快速编辑:

到目前为止,我知道如何将参数传递给函数并不热:

database.js

exports.filterByCategory = function(fnc, arg){
    var whatToFind = {category: arg}

    db.finances.find(whatToFind, function(err, docs){
        fnc(docs)
    })
}

summary.js

database.filterByCategory(function(filter, arg){
    if (filter !== 0){
    for (i = 0; i < filter.length; i++){
        console.log("FILTER: " + filter[i].name)
        }
    }
})

了解了如何做,真的很简单:

database.js

exports.filterByCategory = function(name, fnc){
    var arg = {
        "name": name
    }
    db.finances.find({category: arg.name}, function(err, docs){
        fnc(docs)
    })
}

summary.js

database.filterByCategory(arg, function(fnc){
    if (fnc !== 0){
        for (i = 0; i < fnc.length; i++){
            console.log("object: " + fnc[i].name)
        }
    }
})

0 个答案:

没有答案