[英]Mongoose query is returning 'undefined'`
我正在使用以下猫鼬语法进行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'}
您必须先定义架构 。
请参阅说明文件
然后设置
var mongoose = require('mongoose')
, query = mongoose.model('ex', exSchema);
另外你必须做连锁电话
尝试
query
.where('fname', fName)
.where('lname', lName)
.exec(function(err, results) {
// do some with results
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.