0

This router get function is entered, the log statement is printed to the console, but the find statement doesn't seem to be executing. 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;
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
koque
  • 1,830
  • 5
  • 28
  • 49

2 Answers2

2

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. 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. Using mongoose.connect() will solve your problem. You can read about it here http://mongoosejs.com/docs/api.html#index_Mongoose-connect

  • Thank you. Worked like a charm. I was using mongoose.connect previously but it told me it was deprecated and suggested mongoose.createConnection(). I will have to beef up on this. – koque Jul 29 '17 at 00:00
1

There are some differences when you use .createConnection instead of .connect.

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

Amit G.
  • 323
  • 3
  • 8