首页 > 解决方案 > (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 进程。

有人能帮我吗?

标签: javascriptnode.jsshellcommand-line

解决方案


推荐阅读