2012-07-28 109 views
1

我使用下面的貓鼬語法制作蒙戈查詢....貓鼬查詢返回「undefined'`

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

我正在運行此查詢了。如果,否則,如果,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文檔看起來像一個樣......

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

你能澄清你的問題嗎?我無法弄清楚你在問什麼。當你說:'查詢返回這個....',是在客戶端?或者是服務器端的這些錯誤? – JohnnyHK 2012-07-28 14:32:22

+0

錯誤在服務器端。它發生在我爲'lname'進行數據庫查詢時發生的錯誤是它的未定義。但是,當我查詢數據庫'fname'和'lname'它工作正常,以及查詢'fname'。 – 2012-07-28 16:26:00

+0

錯誤在服務器端。它發生在web上我正在爲'lname'進行數據庫查詢,錯誤在於它未定義。但是,當我查詢數據庫'fname'和'lname'它工作正常,以及查詢'fname'。 – 2012-07-28 16:26:08

回答

0

你必須定義模式第一。

Documentation

然後設置

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

你也需要做鏈調用

嘗試

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

謝謝,但我已經有了一個架構,並且在查詢'fname'和'lname'時它工作正常。但問題是當我只想查詢'lname'時。當我用'fname'進行查詢時,它工作得很好,但不能與'lname' – 2012-07-28 21:37:11