我正在使用以下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);
}
})
以下是DB doc的样子......
{ fname: 'First Name',
lname: 'Last Name'}
答案 0 :(得分: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
});