1

我正在使用以下 mongoose 语法进行 mongo 查询....

                // Log the search
                console.log("Searching for: " + lName);

                query.where('lname', lName)
                query.exec(function(err,results){
                        //Check for an err
                        if(err){
                                // Send the err
                                res.send(err);

                                // Log the err
                                console.log(err);
                        } else { 
                                // Send the query results
                                res.send(results);

                                // Log the results
                                console.log(results);
                        }
                });

查询返回这个....

URL: /api/search/customers/?lname=Last+Name // My variable
Query contents = Last Name // My variable
ReferenceError: lname is not defined
    at /home/collin/Documents/code/webdev/loyalty-app/api.js:311:12
    at callbacks (/home/collin/node_modules/express/lib/router/index.js:272:11)
    at param (/home/collin/node_modules/express/lib/router/index.js:246:11)
    at pass (/home/collin/node_modules/express/lib/router/index.js:253:5)
    at Router._dispatch (/home/collin/node_modules/express/lib/router/index.js:280:4)
    at Object.handle (/home/collin/node_modules/express/lib/router/index.js:45:10)
    at next (/home/collin/node_modules/express/node_modules/connect/lib/http.js:204:15)
    at Object.methodOverride [as handle] (/home/collin/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
    at next (/home/collin/node_modules/express/node_modules/connect/lib/http.js:204:15)
    at Object.bodyParser [as handle] (/home/collin/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:88:61)

我从 If、else if、else 语句中运行这个查询,当我像这样运行它时,查询工作得很好,而且只有名字......

                // Log the search       
                console.log("Searching for: " + fName + "," + lName);

                query.where('fname', fName)
                query.where('lname', lName)
                query.exec(function(err, results){
                        // Check for an error   
                        if(err){
                                //Send the err
                                res.send(err);

                                // Log the error
                                console.log(err);
                        } else{
                                // Send the results of the query
                                res.send(results);

                                // Log the results
                                console.log(results);
                        }
                })

这是数据库文档的示例...

 { fname: 'First Name',
   lname: 'Last Name'}
4

1 回答 1

0

您必须先定义架构

查看文档

然后设置

var mongoose = require('mongoose')
, query = mongoose.model('ex', exSchema);

你也必须做链式调用

尝试

query
.where('fname', fName)
.where('lname', lName)
.exec(function(err, results) {
    // do some with results
});
于 2012-07-28T18:11:01.823 回答