0

I'm working on a module to manually import data that was lost during a db upgrade. I created the JSON, and did a mongoimport into a collection named loaddata.

From the mongo shell, running db.loaddata.find() returns all 18 orders just like they should be.

When I attempt to run it from node, I get 0 orders (no error) but I don't get why it isn't returning the results, except that I maybe messed up with the database model? Here is my code:

module.exports.lostOrdersGetAll = function(req, res) {
  console.log('GET the lost orders');
  LostData.find({}).exec(function(err, orders) {
    if (err) {
      console.log('Error finding lost orders');
      res.status(500).json(err);
    } else {
      console.log('Found lost orders', orders.length);
      res.json(orders);
    }
  });
};

My Model

var mongoose = require('mongoose');

var lostdataSchema = new mongoose.Schema({
  cart: {
    username: String,
    items: [],
    totalQty: Number,
    totalPrice: Number
  },
  customer: String,
  paymentID: {
    type: String,
    required: true
  },
  paymentNote: String,
  refundedAmt: Number,
  paymentStatus: {
    type: String
  },
  user: String,
  createdOn: {
    type: Date,
    default: Date.now
  },
  updatedOn: {
    type: Date
  },
  active: Boolean
});

mongoose.model('LostData', lostdataSchema);

My db.js file, although I doubt the problem is in this file:

var mongoose = require('mongoose');
var options = {
  useMongoClient: true
};
const dburl = 'mongodb://localhost:27017/rumble';

var retry = null;

mongoose.connect(dburl, options);

//CONNECTION EVENTS
mongoose.connection.on('connected', function() {
  console.log('Mongoose connected to', dburl);
});

mongoose.connection.on('disconnected', function() {
  console.log('Mongoose disconnected', dburl);
});

mongoose.connection.on('error', function(err) {
  console.log('Mongoose connection error', err);
});
//CAPTURE APP TERMINATION / RESTART EVENTS
//To be called when process is restarted or terminated
function gracefulShutdown(msg, callback) {
  mongoose.connection.close(function() {
    console.log('Mongoose disconnected through ' + msg);
    callback();
  });
}

//For nodemon restarts
process.once('SIGUSR2', function() {
  gracefulShutdown('nodemon restart', function() {
    process.kill(process.pid, 'SIGUSR2');
  });
});
//For app termination
process.on('SIGINT', function() {
  gracefulShutdown('App termination (SIGINT)', function() {
    process.exit(0);
  });
});
//For Heroku app termination
process.on('SIGTERM', function() {
  gracefulShutdown('App termination (SIGTERM)', function() {
    process.exit(0);
  });
});

//bring in schemas and models
require('./admins.model.js');
require('./articles.model.js');
require('./orders.model.js');
require('./lostdata.model.js');
require('./fields.model.js');
require('./pages.model.js');
require('./alerts.model.js');
require('./products.model.js');
require('./registrations.model.js');
require('./users.model.js');
require('./volunteers.model.js');
require('./winners.model.js');
require('./stripepayments.model.js');
require('./stripecharges.model.js');
require('./cartstore.model');
require('./waiverrecipients.model');

The logged results from the console (to me, this says the code is running correctly and did not return errors) are: Console logs for running module.exports.lostOrdersGetAll My Mongo Shell results: 18 records found, as expected

1
  • from mongo console, what is the response for this command show collections? Commented May 31, 2019 at 20:04

1 Answer 1

2

for mongoose.model() method

The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.

Mongoose trying to be smart and detect the model name, but you can force it to use the collection you want like this

new Schema({..}, { collection: 'lostdata' })

Or when you create the model, like this

mongoose.model('LostData', lostdataSchema, 'lostdata')
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU!!! It's been such a long time since I wrote a mongoose model, and forgot about that. You saved my day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.