UnhandledPromisejectionWarning:我收到未处理的承诺拒绝错误我正在使用node js和mongo db

xj3cbfub  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(360)

当我尝试连接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.

下面是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}`))

任何帮助都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题