【问题标题】:got an error in Unhandled promise rejection在未处理的承诺拒绝中出现错误
【发布时间】:2021-02-15 05:15:33
【问题描述】:

当我尝试在本地主机中发布表单时,vscode 终端出现错误。我正在为我的网站使用 mongodb 作为数据库。以下是我的 app.js 代码-

const express = require("express");
const path = require("path"); 
const bodyparser = require("body-parser");
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/contactdance', {useNewUrlParser: true, useUnifiedTopology: true});

        const app = express();
const port = 8000;

//DEFINE MONGOOSE SCHEMA
const contactSchema = new mongoose.Schema({
    name: String,
    phone: String,
    email: String,
    address: String,
    desc: String
  });

  const contact = mongoose.model('Contact', contactSchema);

// EXPRESS SPECIFIC STUFF
app.use('/static', express.static('static')) // For serving static files
app.use(express.urlencoded())

// PUG SPECIFIC STUFF
app.set('view engine', 'pug') // Set the template engine as pug
app.set('views', path.join(__dirname, 'views')) // Set the views directory
 
// ENDPOINTS
app.get('/', (req, res)=>{ 
    const params = { }
    res.status(200).render('home.pug', params);
})

app.get('/contact', (req, res)=>{ 
    const params = { }
    res.status(200).render('contact.pug', params);
})

app.post('/contact', (req, res)=>{ 
    var myData = new contact(req.body);
    myData.save().then(()=>{
        res.send("This item has been saved successfully")
    }).catch(()=>{
        res.status(400).send("Item has not been saved")
    });

    
    // res.status(200).render('contact.pug');
});

// START THE SERVER
app.listen(port, ()=>{
    console.log(`The application started successfully on port ${port}`);
});

以下是尝试发布表单时的错误-

(node:15248) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\connection.js:803:32)
    at E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\index.js:342:10
    at E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
    at Mongoose.connect (E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\index.js:341:10)
    at Object.<anonymous> (E:\program files\rr\Web Development\dance_website\app.js:5:10)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
(node:15248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate 
the Node.js process with a non-zero exit code.

下面是我的 package.json-

{
  "name": "dance_website",
  "version": "1.0.0",
  "description": "This is a website for a polular dance academy",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Harry bhai",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongoose": "^5.10.11",
    "pug": "^3.0.0"
  },
  "devDependencies": {}
}

我在我的 vscode 中打开了三个终端-

  1. 对于nodejs

  2. 对于蒙哥

  3. 对于 mongo 但是当我在终端中打开 mongo 时出现错误。以下是错误-

    MongoDB shell 版本 v4.4.1 连接到:mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb 错误:无法连接到服务器 127.0.0.1:27017,连接尝试失败:SocketException:连接到 127.0.0.1:27017 :: 时出错,由 :: 目标机器主动拒绝,无法建立连接。 : 连接@src/mongo/shell/mongo.js:374:17 @(连接):2:6 异常:连接失败 退出代码 1

【问题讨论】:

  • ECONNREFUSED 127.0.0.1:27017 您的 MongoDB 服务器没有运行。启动您的 MongoDB 服务。
  • 正如我已经提到的,我首先在一个终端打开 mongod,在第二个终端打开 mongo,然后在不同的终端运行 nodejs。之后我正在尝试提交帖子。当我尝试提交帖子时会发生错误。
  • 你能说一下你用的是什么机器吗?视窗?苹果? Linux?

标签: javascript node.js json mongodb mongoose


【解决方案1】:

您收到的两个错误表示同一件事:您的 MongoDB 服务器没有运行。

仔细检查 mongo 本身的安装说明,而不是 mongoose 或 mongo shell。

当一台机器在你自己的127.0.0.1 机器中“拒绝”与 ECONNREFUSED 的连接时,这意味着没有服务器正在侦听特定端口。在您的情况下为 27017。您可能还有其他配置问题,但您肯定有那个问题。

您可以使用网络浏览器访问 http://localhost:28017 以查看您的服务器是否也在运行。

【讨论】:

  • 为了连接 mongo 和 nodejs,我们使用 mongoose。我已经在“npm install mongoose”的帮助下安装了 mongoDB 并在我的 package.json 中安装了 mongoose。我认为原因在别处
  • @RaviKumar 我认为@OJones 的意思是您应该导航到您的mongod.exe 所在的位置(或将其放在PATH 上)并运行它以使服务器运行。我不确定在 Mac 上是如何工作的,但这是一般的方法。
  • @RaviKumar 安装 Mongoose 并不意味着您的 MongoDB 实例正在运行。 Mongoose 是一个 Javascript 库,它与您的 MongoDB 服务器是否运行无关。打开浏览器,转到http://localhost:27017,您应该会看到一条消息,说明您正在尝试通过 HTTP 访问 MongoDB。如果你不这样做,你的 MONGODB 服务器。是。不是。跑步。如果你坚持说问题出在其他地方,那为什么要来 Stackoverflow 寻求帮助,然后拒绝每个人告诉你并同意的内容?
猜你喜欢
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
相关资源
最近更新 更多