Server is starting at 8080 GET / 304 - - 1.952 ms (node:6139) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: bad auth Authentication failed. at new MongooseServerSelectionError (/Users/abc/Desktop/success/node_modules/mongoose/lib/error/serverSelection.js:22:11) at NativeConnection.Connection.openUri (/Users/abc/Desktop/success/node_modules/mongoose/lib/connection.js:808:32) at Mongoose.connect (/Users/abc/Desktop/success/node_modules/mongoose/lib/index.js:333:15) at Object. (/Users/abc/Desktop/success/pages/server.js:18:10) at Module._compile (internal/modules/cjs/loader.js:1158:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) at Function.Module._load (internal/modules/cjs/loader.js:901:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 (node:6139) 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:6139) [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. GET / 304 - - 0.320 ms GET / 304 - - 0.236 ms GET / 304 - - 0.210 ms
When I run Port 8080 for my serverjs, the file works but no data gets into my mongoDB and I get the above error on my terminal. I only have a server.js page and an index.tsx page. My index.tsx page has a simple Hello world and I am trying to connect Mongodb using the server.js file.
Here is my code:
import express from 'express';
import { connect, connection, Schema as _Schema, model } from 'mongoose';
import morgan from 'morgan';
import path from 'path';
const app = express();
const PORT = process.env.PORT || 8080;
//Success
const MONGODB_URI = 'mongodb+srv://xxxxxxxxxx';
mongoose.connect(MONGODB_URI || 'mongodb://localhost/success', {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on('connected', () => {
console.log('Mongoose is connected');
})
//Schema
const Schema = _Schema;
const BlogPostSchema = new Schema({
title: String,
body: String,
date: {
type: String,
default: Date.now()
}
});
//Model
const BlogPost = model('BlogPost', BlogPostSchema);
//Saving data to our mongoDB
const data = {
title: 'welcome',
body: 'first post'
};
const newBlogPost = new BlogPost(data);
newBlogPost.save((error) => {
if (error) {
console.log("Somethign happened");
} else {
console.log("data saved");
}
});
//HTTP request logger
app.use(morgan('tiny'));
//Routes
app.get('/', (req, res) => {
const data = {
username: 'caa',
age: 5
};
res.json(data);
});
app.get('/api/name', (req, res) => {
const data = {
username: 'caa',
age: 5
};
res.json(data);
});
app.listen(PORT, console.log(`Server is starting at ${PORT}`));
MONGODB_URI
? – Tunmee