[英]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.