javascript - (node:17453) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect
问题描述
我有问题,这是我的 logic.js 代码
const mongoose = require('mongoose'); // An Object-Document Mapper for Node.js
const assert = require('assert'); // N.B: Assert module comes bundled with Node.js.
mongoose.Promise = global.Promise; // Allows us to use Native promises without throwing error.
// Connect to a single MongoDB instance. The connection string could be that of a remote server
// We assign the connection instance to a constant to be used later in closing the connection
const db = mongoose.connect('mongodb://localhost:27017/contact-manager', { useNewUrlParser: true });
// Converts value to lowercase
function toLower(v) {
return v.toLowerCase();
}
// Define a contact Schema
const contactSchema = mongoose.Schema({
firstname: { type: String, set: toLower },
lastname: { type: String, set: toLower },
phone: { type: String, set: toLower },
email: { type: String, set: toLower }
});
// Define model as an interface with the database
const Contact = mongoose.model('Contact', contactSchema);
/**
* @function [addContact]
* @returns {String} Status
*/
const addContact = (contact) => {
Contact.create(contact, (err) => {
assert.equal(null, err);
console.info('New contact added');
db.disconnect();
});
};
/**
* @function [getContact]
* @returns {Json} contacts
*/
const getContact = (name) => {
// Define search criteria. The search here is case-insensitive and inexact.
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();
});
};
// Export all methods
module.exports = { addContact, getContact };
这是用于contact.js
const program = require('commander');
// Require logic.js file and extract controller functions using JS destructuring assignment
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);
当我在终端'node contact.js addContact John Doe 013-452-3134 john.doe@contacto.com'中运行时,它显示:
(node:17453) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] at Pool。(/home/akbar/projecttt/node_modules/mongodb-core/lib/topologies/server.js:564:11) 在 Pool.emit (events.js:182:13) 在 Connection。(/home/akbar/projecttt/node_modules/mongodb-core/lib/connection/pool.js:317:12) 在 Object.onceWrapper (events.js:273:13) 在 Connection.emit (events.js:182: 13)在插座。(/home/akbar/projecttt/node_modules/mongodb-core/lib/connection/connection.js:246:50) 在 Object.onceWrapper (events.js:273:13) 在 Socket.emit (events.js:182: 13) 在emitErrorNT (internal/streams/destroy.js:82:8) 在emitErrorAndCloseNT (internal/streams/destroy.js:50:3) 在进程。_tickCallback (internal/process/next_tick.js:63:19) (node:17453) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。(拒绝 ID:1)(节点:17453)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。
有人能帮我吗?
解决方案
推荐阅读
- pandas - 如何根据条件行值对 pandas 数据框进行取消堆叠或取消透视?
- mysql - 用于返回没有活动组成员身份的用户的 SQL 查询
- java - 如何将 Liquibase 与 Spring-Boot JPA 和 Maven 一起使用来管理来自 JPA 实体的迁移
- ruby-on-rails - ArgumentError:升级 Rails 6.1.3 时出现错误数量的参数错误
- powershell - 如何传入 .txt 文件以在 PowerShell 中创建类型字符串列表
- flutter - 无法在字段初始化程序中访问“this”以读取“assists”
- javascript - Firebase Emulator 无法在 Firefox 中建立连接
- r - 获取 R 中的估计后边际概率
- javascript - Javascript 在鼠标状态为“保持”时调用函数
- react-native - 更改子组件的 props 值导致渲染过多