Reputation: 846
I'm using this tutorial to make a node/mongo application. When I run addContact, it seems like the contact saves to the DB but, I get an error saying that the disconnect function is not a function. Can anyone tell me why this is happening and, how to fix it? I'm assume that there's some issue with the scope of the constant, db?
code:
const mongoose = require('mongoose');
const assert = require('assert');
mongoose.Promise = global.Promise;
const db = mongoose.connect('mongodb://localhost:27017/contact-manager');
function toLower(v) {
return v.toLowerCase();
}
const contactSchema = mongoose.Schema({
firstname: { type: String, set: toLower },
lastname: { type: String, set: toLower },
phone: { type: String, set: toLower },
email: { type: String, set: toLower }
});
const Contact = mongoose.model('Contact', contactSchema);
const addContact = (contact) => {
Contact.create(contact, (err) => {
assert.equal(null, err);
console.info('New contact added');
db.disconnect();
});
};
const getContact = (name) => {
const search = new RegExp(name, 'i');
Contact.find({$or: [{firstname: search }, {lastname: search }]})
.exec((err, contact) => {
assert.equal(null, err);
console.info(contact);
console.info(`${contact.length} matches`);
db.disconnect();
});
};
module.exports = { addContact, getContact };
code part2:
const program = require('commander');
const {addContact, getContact} = require('./logic');
program
.version('0.0.1')
.description('Contact management system');
program
.command('addContact <firstame> <lastname> <phone> <email>')
.alias('a')
.description('Add a contact')
.action((firstname, lastname, phone, email) => {
addContact({firstname, lastname, phone, email});
});
program
.command('getContact <name>')
.alias('r')
.description('Get contact')
.action(name => getContact(name));
program.parse(process.argv);
error:
New contact added
/Users/user/contact-manager/node_modules/mongodb/lib/utils.js:132
throw err;
^
TypeError: db.disconnect is not a function
at Contact.create (/Users/user/contact-manager/logic.js:33:8)
at Function.<anonymous> (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:3913:16)
at parallel (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:2077:12)
at /Users/user/contact-manager/node_modules/async/internal/parallel.js:35:9
at /Users/user/contact-manager/node_modules/async/internal/once.js:12:16
at iteratorCallback (/Users/user/contact-manager/node_modules/async/eachOf.js:52:13)
at /Users/user/contact-manager/node_modules/async/internal/onlyOnce.js:12:16
at /Users/user/contact-manager/node_modules/async/internal/parallel.js:32:13
at apply (/Users/user/contact-manager/node_modules/lodash/_apply.js:15:25)
at /Users/user/contact-manager/node_modules/lodash/_overRest.js:32:12
at callbackWrapper (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:2046:11)
at /Users/user/contact-manager/node_modules/mongoose/lib/model.js:3913:16
at model.$__save.error (/Users/user/contact-manager/node_modules/mongoose/lib/model.js:342:7)
at /Users/user/contact-manager/node_modules/kareem/index.js:297:21
at next (/Users/user/contact-manager/node_modules/kareem/index.js:209:27)
at Kareem.execPost (/Users/user/contact-manager/node_modules/kareem/index.js:217:3)
Upvotes: 2
Views: 4100
Reputation: 396
mongoose.connect
does not return a value, as far as I can tell from looking at the code given in the getting started guide. If you want to close the database connection, you need to call the close
method on mongoose.connection
which is written to when you open a connection with mongoose.connect
. The simplest way to do this is to move your mongoose.connect
to its own line:
mongoose.connect('mongodb://localhost:27017/contact-manager');
const db = mongoose.connection;
Then just replace your calls to db.disconnect()
with calls to db.close()
.
Upvotes: 10
Reputation:
Try letting mongoose handle the connection closure using mongoose.connection.close()
Upvotes: 3