(节点:17453)UnhandledPromiseRejectionWarning:MongoNetworkError:首次连接时无法连接到服务器[localhost:27017]

时间:2018-12-19 03:26:47

标签: javascript node.js shell command-line

我有问题,这是我的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”中运行时,它显示以下内容:

(节点:17453)UnhandledPromiseRejectionWarning:MongoNetworkError:首次连接时无法连接到服务器[localhost:27017] [MongoNetworkError:连接ECONNREFUSED 127.0.0.1:27017]     在游泳池。 (/home/akbar/projecttt/node_modules/mongodb-core/lib/topologies/server.js:564:11)     在Pool.emit(events.js:182:13)     在连接。 (/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上(内部/流/destroy.js:82:8)     在emitErrorAndCloseNT(internal / streams / destroy.js:50:3)     在process._tickCallback(内部/进程/next_tick.js:63:19) (节点:17453)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1) (节点:17453)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

有人可以帮我吗?

0 个答案:

没有答案