我在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 clonewhatsapp-backendnode_modulesmongodblibcoretopologiesserver.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 clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.js:362:19)
at Connection.emit (node:events:390:28)
at processMessage (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.js:454:10)
at TLSSocket.<anonymous> (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.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 clonewhatsapp-backendnode_modulesmongodblibcoretopologiesserver.js:441:11)
at Pool.emit (node:events:390:28)
at E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionpool.js:564:14
at E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionpool.js:1013:9
at callback (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnect.js:75:5)
at E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnect.js:147:27
at E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreauthscram.js:109:14
at _callback (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.js:331:7)
at Connection.messageHandler (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.js:362:9)
at Connection.emit (node:events:390:28)
at processMessage (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.js:454:10)
at TLSSocket.<anonymous> (E:Whatapp clonewhatsapp-backendnode_modulesmongodblibcoreconnectionconnection.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:asdfghjkl1233@cluster0.ngclm.mongodb.net/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模式(dbMessages.js(
import mongoose from "mongoose";
const whatsappSchema = mongoose.Schema({
message:String,
name:String,
timestamp:String
});
export default mongoose.model('whatsapp', whatsappSchema);
我的包裹。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"
}
}
删除此行并运行:
import react from 'react';
从Server.js文件中删除import react from 'react';
,因为服务器端不知道react是什么。