I have created a WhatsApp clone in the backend of Whatsapp clone i am facing following errors. when i run yarn start i am facing following errors in console.
yarn start
yarn run v1.23.0-20200615.1913
$ node server.js
(node:4316) [MONGODB DRIVER] Warning: the options [useUnifedTopology] is not supported
(Use `node --trace-warnings ...` to show where the warning was created)
listen on local host 6000
(node:4316) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\topologies\server.js:441
new MongoNetworkError(
^
MongoNetworkError: failed to connect to server [cluster0-shard-00-01.ngclm.mongodb.net:27017] on first connect [MongoError: bad auth : Authentication failed.
at Connection.messageHandler (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:362:19)
at Connection.emit (node:events:390:28)
at processMessage (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:454:10)
at TLSSocket.<anonymous> (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:623:15)
at TLSSocket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:199:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}]
at Pool.<anonymous> (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\topologies\server.js:441:11)
at Pool.emit (node:events:390:28)
at E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\pool.js:564:14
at E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\pool.js:1013:9
at callback (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connect.js:75:5)
at E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connect.js:147:27
at E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\auth\scram.js:109:14
at _callback (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:331:7)
at Connection.messageHandler (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:362:9)
at Connection.emit (node:events:390:28)
at processMessage (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:454:10)
at TLSSocket.<anonymous> (E:\Whatapp clone\whatsapp-backend\node_modules\mongodb\lib\core\connection\connection.js:623:15)
at TLSSocket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
My main file Server.js (It is the main file )
// Import
import express from "express";
import mongoose from "mongoose";
import Messages from './dbMessages.js';
import Pusher from "pusher";
import cors from "cors";
import axios from "axios";
// app config
const app = express();
const port = process.env.PORT || 6000;
// Pusher code
const pusher = new Pusher({
appId: "1366318",
key: "eb9c7412499f6e7b4c5f",
secret: "6903a1ffa86fcb2c3ddd",
cluster: "ap2",
useTLS: true
});
// middle ware
app.use(express.json());
app.use(cors());
app.use((req,res, next ) => {
res.setHeader("Access-Control-Allow-Origin" ,"*");
res.setHeader( "Access-Control-Allow-Origin" ,"*");
next();
});
// DB Config
const connection_url='mongodb+srv://whatsapp:[email protected]/whatsapp?retryWrites=true&w=majority';
mongoose.connect(connection_url,{
useCreateIndex: true,
useNewUrlParser: true,
useUnifedTopology: true
})
// ?????
const db = mongoose.connection;
db.once("open",()=>{
console.log("db connected");
// message contents is from mongodb database
const msgCollection = msgCollection('whatsapp'); //collection name in mongodb messagecontents
// messagecontents changing to whatsapp
const changeStream = msgCollection.watch();
// Change Stream function
changeStream.on("change", (change) =>
{
// this is when we troggling pusher
console.log('A changed is occur',change);
if (change.operationType === 'insert'){
const messageDetails = change.fullDocument;
pusher.trigger('message', 'insert', {
name:messageDetails.user,
message: messageDetails.message,
timeStamp: messageDetails.timeStamp,
received: messageDetails.received
});
} else {
console.log('Error Troggling pusher');
}
});
});
// api router
app.get('/',(req,res) => {
res.status(200).send("Hello World");
});
// api to get all the data from database
app.get('/messages/sync' , (req,res) => {
// Messsages.find() to get back all the messages
Messages.find((err,data) => {
if (err) {
res.status(500).send(err)
}
else {
res.status(200).send(data)
}
});
});
app.post('/api/messages/new', (res,req) => {
const dbMessage = req.body
message.create(dbMessage, (err,data)=>{
if (err){
res.status(500).send(err)
}
else {
res.status(201).send(`new number created: \n ${data}`)
}
});
});
// listen
app.listen(port, () => console.log(`listen on local host ${port}`));
MY schema (dbMessages.js)
import mongoose from "mongoose";
const whatsappSchema = mongoose.Schema({
message:String,
name:String,
timestamp:String
});
export default mongoose.model('whatsapp', whatsappSchema);
My Package.Json
{
"name": "whatsapp-backend",
"version": "1.0.0",
"discription": "",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"nodemon":"nodemon --experimental-modules --es-module-specifier-resolution=node "
},
"keywords": [],
"author": "zain",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.3",
"express_js": "^1.0.0",
"mongoose": "^6.2.5",
"nodemon": "^2.0.15",
"pusher": "^5.0.1",
"pusher-js": "^7.0.6"
}
}
Remove this line and run: