动态设置对象中的键

时间:2012-11-05 23:16:22

标签: javascript node.js mongodb-query

我尝试在MongoDB中为Node.js动态构建查询。创建它的函数是:

    app.set('searchTerm', function (field, str){
        var i, searchTerm, keywords;
        keywords = str.split(' ');
        searchTerm = {field : str , _keywords : keywords};
        return searchTerm;
    });

我的问题是构造的对象最终会出现字符串'字段'作为关键,而不是传递的论点。如何评估论证?

1 个答案:

答案 0 :(得分:2)

app.set('searchTerm', function (field, str){
    var i, searchTerm, keywords;
    keywords = str.split(' ');
    searchTerm = {_keywords: keywords};
    searchTerm[field] = str;
    console.dir(searchTerm);
    return searchTerm;
});