简体   繁体   中英

Mongoose query is returning 'undefined'`

I am making a mongo query using the following mongoose syntax....

                // 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);
                        }
                });

The query is returning this....

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)

I am running this query out of a If, else if, else statement and the query works perfect when I run it like this, and with just the first name...

                // 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);
                        }
                })

Here is a sample of what the DB doc looks like...

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

You have to define Schema first.

See Documentation

And then set

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

Also you have to do chain calling

Try

query
.where('fname', fName)
.where('lname', lName)
.exec(function(err, results) {
    // do some with results
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM