2017-07-28 52 views
0

這個路由器的get函數被輸入,日誌語句被打印到控制檯,但find語句似乎沒有執行。任何明顯的原因?Mongoose:查找語句沒有執行

const express = require('express'); 
const router = express.Router(); 
const mongoose = require('mongoose'); 

const Product = require('../models/product'); 

module.exports = router; 

const url = "mongodb://[email protected]:xxxx/xxxx"; 

mongoose.Promise = global.Promise; 
mongoose.createConnection(url, function(err) { 
    if(err) { 
     console.log('Error!!!' + err); 
    } else { 
     console.log('Connected to Database!'); 
    } 
}); 

router.get('/product/specialvalue', function(req, res) { 
    console.log('Get specialvalue called xxxx'); 

    Product.find({'special_value': true}) 
    .sort({'price': 1}) 
    .exec(function(err, products) { 
     if(err) { 
      console.error('Error retrieving special value products!'); 
      res.json(err); 
     } else { 
      console.log("products = " + JSON.stringify(products)); 
      res.json(products);   
     } 
    }); 

}); 

這是貓鼬型號:

const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 

const ProductSchema = new Schema({ 
    producttype: String, 
    name: String, 
    brand: String, 
    model: String, 
    price: Number, 
    list_price: Number, 
    description: String, 
    rating: Number, 

模式的延續:(該系統是引人注目我添加更多詳細信息)

item_no: String, 
    special_value: Boolean, 
    warranty: String, 
    feature: [String], 
    image: [String], 

延續型號:

specification: { 
    lowes_exclusive : Boolean, 
    color : String, 
    high_efficiency : Boolean, 
    automatic_load_balancing : Boolean, 
    product_capacity : String, 
    large_items_cycle : Boolean, 

延續模型:

 exclusive_cycle : String, 
     maximum_spin_speed : String, 
     water_levels : String, 
     number_of_rinse_cycles : String 
     } 
    }); 

延續型號:

var Product = mongoose.model('Product', ProductSchema, 'product'); 
module.exports=Product; 

回答

2

你的問題是你如何連接的數據庫。 mongoose.createConnection()將連接對象返回給數據庫,但它不會將其設置爲貓鼬的默認連接。 find查詢未執行的原因是Mongoose將所有數據庫調用排隊,並在處理它們之前等待打開連接,而不是在沒有連接時拋出錯誤。使用mongoose.connect()將解決您的問題。你可以閱讀在這裏http://mongoosejs.com/docs/api.html#index_Mongoose-connect

+0

謝謝你一個很好的解釋。像魅力一樣工作。我之前使用過mongoose.connect,但它告訴我它已被棄用,並建議使用mongoose.createConnection()。我將不得不加強這一點。 – koque

1

當您使用.createConnection,而不是.connect有一定的差異。

這裏是重構的代碼,工程:

const express = require('express'); 
const router = express.Router(); 
const mongoose = require('mongoose'); 
const url = "mongodb://[email protected]:xxxx/xxxx"; 
const Schema = mongoose.Schema; 

mongoose.Promise = global.Promise; 


const db = mongoose.createConnection(url, function(err) { 
    if(err) { 
     console.log('Error!!!' + err); 
    } else { 
     console.log('Connected to Database!'); 
    } 
}); 

const ProductSchema = new Schema({ 
    producttype: String, 
    name: String, 
    brand: String, 
    model: String, 
    ... 
    } 
}); 

const Product = db.model('Product', ProductSchema); 

router.get('/product/specialvalue', function(req, res) { 
    console.log('Get specialvalue called xxxx'); 
    Product.find({'special_value': true}) 
     .sort({'price': 1}) 
     .exec(function(err, products) { 
      if(err) { 
       console.error('Error retrieving special value products!'); 
       res.json(err); 
      } else { 
       console.log("products = " + JSON.stringify(products)); 
       res.json(products); 
      } 
     }); 

}); 

退房this post關於如何使用.createConnection