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