gpt4 book ai didi

javascript - Mongoose "disconnect is not a function"

转载 作者:可可西里 更新时间:2023-11-01 09:56:29 27 4
gpt4 key购买 nike

我正在使用 this tutorial制作 Node/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 };

代码第二部分:

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)

最佳答案

mongoose.connect 没有返回值,据我看 getting started guide 中给出的代码可以看出.如果你想关闭数据库连接,你需要调用 mongoose.connection 上的 close 方法,当你用 mongoose.connect 打开连接时写入该方法。最简单的方法是将 mongoose.connect 移动到它自己的行:

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

然后只需将对 db.disconnect() 的调用替换为对 db.close() 的调用即可。

关于javascript - Mongoose "disconnect is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com