我使用 MERN Stack 创建了一个 Whatsapp 克隆。面对后端错误
我在 Whatsapp 克隆的后端创建了 WhatsApp 克隆,但我面临以下错误。 当我运行yarn start 时,我在控制台中遇到以下错误。
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
(节点:4316)[MONGODB DRIVER] 警告:当前服务器发现和监控引擎已弃用,并将在未来版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。
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)
我的主文件 Server.js (这是主文件)
// 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}`));
我的架构 (dbMessages.js)
import mongoose from "mongoose";
const whatsappSchema = mongoose.Schema({
message:String,
name:String,
timestamp:String
});
export default mongoose.model('whatsapp', whatsappSchema);
我的 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"
}
}
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"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除此行并运行:
Remove this line and run:
从 Server.js 文件中删除
import React from 'react';
因为服务器端不知道 React 是什么。remove
import react from 'react';
from your Server.js file because the server-side doesn't know what react is.