Mongoose:查找语句未执行

时间:2017-07-28 23:00:19

标签: node.js mongoose

输入此路由器get函数,将日志语句打印到控制台,但find语句似乎没有执行。有任何明显的原因吗?

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

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

module.exports = router;

const url = "mongodb://xxxx@ds027425.mlab.com: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);        
        }
    });

});

这是Mongoose模型:

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 个答案:

答案 0 :(得分:2)

您的问题在于如何连接数据库。 mongoose.createConnection()将连接对象返回到数据库,但它不会将其设置为mongoose的默认连接。 find查询未执行的原因是Mongoose对所有数据库调用进行排队,并在处理之前等待打开连接,而不是在没有连接时抛出错误。使用mongoose.connect()可以解决您的问题。你可以在这里阅读http://mongoosejs.com/docs/api.html#index_Mongoose-connect

答案 1 :(得分:1)

使用 .createConnection 而不是 .connect 时会有一些差异。

以下是重构的代码:

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const url = "mongodb://xxxx@ds027425.mlab.com: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

的非常好的解释