简体   繁体   English

猫鼬:查找语句未执行

[英]Mongoose: find statement not executing

This router get function is entered, the log statement is printed to the console, but the find statement doesn't seem to be executing. 输入该路由器的get函数,将log语句打印到控制台,但是find语句似乎没有执行。 Any obvious reasons? 有明显的原因吗?

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

});

This is the Mongoose Model: 这是猫鼬模型:

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,

Continuation of the model: (The system is compelling me to add more detail) 模型的延续:(系统迫使我添加更多细节)

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

Continuation of the model: 模型的延续:

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

Continuation of the model: 模型的延续:

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

Continuation of the model: 模型的延续:

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

Your issue is with how you are connecting the the database. 您的问题在于如何连接数据库。 mongoose.createConnection() returns a connection object to the database, but it does not set it to be mongoose's default connection. mongoose.createConnection()将连接对象返回到数据库,但未将其设置为猫鼬的默认连接。 The reason why the find query is not executing is because Mongoose queues all the DB calls and waits for there to be an open connection before it processes them, instead of throwing an error when there is no connection. 查找查询未执行的原因是,Mongoose将所有数据库调用排队,并在处理它们之前等待打开的连接,而不是在没有连接时引发错误。 Using mongoose.connect() will solve your problem. 使用mongoose.connect()将解决您的问题。 You can read about it here http://mongoosejs.com/docs/api.html#index_Mongoose-connect 您可以在这里http://mongoosejs.com/docs/api.html#index_Mongoose-connect阅读

There are some differences when you use .createConnection instead of .connect . 有一些当你使用的不是.connect .createConnection差异。

Here's the refactored code that works: 这是有效的重构代码:

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

 });

Check out this post for a very good explanation on how to use .createConnection 查看这篇文章 ,以获取有关如何使用.createConnection的很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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