简体   繁体   English

UnhandledPromiseRejectionWarning:未处理的承诺拒绝错误,我正在使用 Node JS 和 mongo Db

[英]UnhandledPromiseRejectionWarning: Unhandled promise rejection error I am getting I am using Node JS and mongo Db

I am getting below error in VS terminal when I am trying to connect with mongo DB当我尝试与 mongo DB 连接时,我在 VS 终端中遇到以下错误

(node:33516) UnhandledPromiseRejectionWarning: Error
    at new RequestError (C:\Users\abhishekkumar-a\Desktop\whatsapp\whatsapp-backend\node_modules\pusher\lib\errors.js:13:16)
    at C:\Users\abhishekkumar-a\Desktop\whatsapp\whatsapp-backend\node_modules\pusher\lib\requests.js:74:13
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:33516) 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:33516) [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.

Below is the code for server.js下面是 server.js 的代码

//importing
import express from 'express';
import mongoose from 'mongoose';
import Messages from './dbMessages.js';
import Pusher from 'pusher';


//app config
const app = express();
const port = process.env.PORT || 9000

const pusher = new Pusher({
    appId: "1239935",
    key: "da6e468b625ab6d6c12d",
    secret: "14e2f50c20842fa7c209",
    cluster: "ap2",
    useTLS: true
});

//middleware 

app.use(express.json());


//DB Config.
const connection_url = 'mongodb+srv://admin:qkDON8dmNgMeZ3fn@cluster0.9p2t6.mongodb.net/whatsappdb?retryWrites=true&w=majority';

mongoose.connect(connection_url,{
    useCreateIndex: true,
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

const db = mongoose.connection;

db.once('open',()=> {
    console.log("DB Connected");

const msgCollection = db.collection("messagecontents");
const changeStream = msgCollection.watch();

changeStream.on('change',(change)=>{
console.log("A change occured",change)
   
    if(change.operationType === 'insert') {
        const messageDetails = change.fullDocument;
        pusher.trigger('messages','inserted',
        {
            name: messageDetails.user,
            message:messageDetails.message
        }
      );
    }  
    else
    {
        console.log("Error triggering pusher");

    }
});
});

//????


//api routes
app.get('/',(req,res)=>res.status(200).send('Hello World'));

app.get('/messages/sync',(req,res) => {
    Messages.find((err,data) => {
        if(err) {
            res.status(500).send(err)
        }
        else {
            res.status(200).send(data)
        }
    })
})


app.post('/messages/new',(req,res) => {
    const dbMessage = req.body

    Messages.create(dbMessage,(err,data) => {
        if(err) {
            res.status(500).send(err)
        }
        else {
            res.status(201).send(data)
        }
    })
})

//listen
app.listen(port,()=>console.log(`Listening on localhost:${port}`))

any help would be appreciated.任何帮助,将不胜感激。

在新的推送器对象创建中试试这个 {cluster: "eu"} 而不是 {cluster: "ap2"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js:未处理的承诺拒绝 - UnhandledPromiseRejectionWarning - Node.js: Unhandled promise rejection - UnhandledPromiseRejectionWarning 即使我的代码中有catch部分,我仍收到未处理的Promise Rejection错误 - I am getting Unhandled Promise Rejection error even though I have the catch section in my code 为什么我在使用带有 Angular 8 的 Pdfmake 时收到“未处理的 promise 拒绝类型错误:null 不是函数”? - Why I am getting "Unhandled promise rejection TypeError: null is not a function" when using Pdfmake with Angular 8? 为什么会收到UnhandledPromiseRejectionWarning:未处理的承诺被拒绝? - Why I get UnhandledPromiseRejectionWarning: Unhandled promise rejection? UnhandledPromiseRejectionWarning:Node.JS中未处理的承诺拒绝(拒绝ID:1) - UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1) in Node.JS 我总是得到 JSON 错误和未处理的承诺拒绝警告的循环结构。 我该如何摆脱它? - I am always getting circular structure to JSON error and unhandled promise rejection warning. How do I get rid of it? UnhandledPromiseRejectionWarning:来自 Node JS 的未处理的承诺拒绝,React 中的预检响应 - UnhandledPromiseRejectionWarning: Unhandled promise rejection from Node JS, Preflight response in React 为什么我收到“可能未处理的 Promise 拒绝 (id:0):”控制台警告以及如何摆脱它 - Why am I getting a "Possible Unhandled Promise Rejection (id:0):" console warning and how to get rid of it (节点:52585)UnhandledPromiseRejectionWarning:未处理的 promise 拒绝 - (node:52585) UnhandledPromiseRejectionWarning: Unhandled promise rejection (节点:2256)UnhandledPromiseRejectionWarning:未处理的 promise 拒绝 - (node:2256) UnhandledPromiseRejectionWarning: Unhandled promise rejection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM