猫鼬“脱离不是一种功能”

时间:2018-01-27 22:07:06

标签: javascript node.js mongodb mongoose

我正在使用this tutorial制作节点/ mongo应用程序。当我运行 addContact 时,似乎联系人保存到数据库但是,我得到一个错误,说断开连接功能不是一个功能。任何人都可以告诉我为什么会这样,以及如何解决它?我假设常量的范围存在一些问题, db

代码:

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

代码第2部分:

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

错误:

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)

2 个答案:

答案 0 :(得分:9)

getting started guide中给出的代码而言,

mongoose.connect不会返回值。如果要关闭数据库连接,则需要在close上调用mongoose.connection方法,该方法在您使用mongoose.connect打开连接时写入。最简单的方法是将mongoose.connect移到自己的行:

mongoose.connect('mongodb://localhost:27017/contact-manager');
const db = mongoose.connection;

然后只需拨打db.disconnect(),即可将db.close()的来电替换为。{/ p>

答案 1 :(得分:3)

尝试让mongoose使用mongoose.connection.close()

处理连接关闭