1

I'm trying to dynamically build a query for MongoDB in Node.js. The function that creates it is:

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

My problem is that the object constructed ends up with the string 'field' as a key, not the passed argument. How do I get it to evaluate the argument?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
user1775718
  • 1,369
  • 2
  • 15
  • 29

1 Answers1

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;
});
user1775718
  • 1,369
  • 2
  • 15
  • 29
  • You could use: `searchTerm = {_keywords: str.split(' ')};` to make the `keywords` variable redundant. :-) – RobG Nov 06 '12 at 02:15