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:
My Mongo Shell results:

show collections?