help is resolving a mongodb issue
by ultra99 from LinuxQuestions.org on (#4T8DS)
I'm learning to code and using mongodb as backend db.
Here's the code:
Code: var bodyParser = require("body-parser"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
//APP CONFIG
mongoose.set('useUnifiedTopology', true);
mongoose.connect("mongodb://localhost:27017/restful_blog_app", {useNewUrlParser: true});
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
//MONGOOSE/MODEL CONFIG
var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: {type: Date, default: Date.now}
});
var Blog = mongoose.model("Blog", blogSchema);
Blog.create({
title: "Test Blog",
image: "https://images.unsplash.com/photo-1517694712202-14dd9538aa97?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
body: "HELLO THIS IS A BLOG POST!"
});
//RESTFUL ROUTES
app.get("/", function(req, res){
res.redirect("/blogs")
});
app.get("/blogs", function(req, res){
Blog.find({}, function(err, blogs){
if(err){
console.log("ERROR!");
} else {
res.render("index", {blogs: blogs});
}
});
})
app.get("/blogs", function(req, res){
res.render("index")
});
app.listen(3000, function(){
console.log("SERVER IS RUNNING!");
});
And this is the error in terminal:
Code: /RESTfulBlogApp> node app.js
SERVER IS RUNNING!
(node:22323) UnhandledPromiseRejectionWarning: MongoError: there are no users authenticated
at Connection.<anonymous> (/RESTful Routing/RESTfulBlogApp/node_modules/mongodb/lib/core/connection/pool.js:454:61)
at Connection.emit (events.js:198:13)
at processMessage (/RESTful Routing/RESTfulBlogApp/node_modules/mongodb/lib/core/connection/connection.js:368:10)
at Socket.<anonymous> (/RESTful Routing/RESTfulBlogApp/node_modules/mongodb/lib/core/connection/connection.js:537:15)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:22323) 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(). (rejection id: 1)
(node:22323) [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.Note: I following this to install MongoDB in openSUSE.


Here's the code:
Code: var bodyParser = require("body-parser"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
//APP CONFIG
mongoose.set('useUnifiedTopology', true);
mongoose.connect("mongodb://localhost:27017/restful_blog_app", {useNewUrlParser: true});
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
//MONGOOSE/MODEL CONFIG
var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: {type: Date, default: Date.now}
});
var Blog = mongoose.model("Blog", blogSchema);
Blog.create({
title: "Test Blog",
image: "https://images.unsplash.com/photo-1517694712202-14dd9538aa97?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
body: "HELLO THIS IS A BLOG POST!"
});
//RESTFUL ROUTES
app.get("/", function(req, res){
res.redirect("/blogs")
});
app.get("/blogs", function(req, res){
Blog.find({}, function(err, blogs){
if(err){
console.log("ERROR!");
} else {
res.render("index", {blogs: blogs});
}
});
})
app.get("/blogs", function(req, res){
res.render("index")
});
app.listen(3000, function(){
console.log("SERVER IS RUNNING!");
});
And this is the error in terminal:
Code: /RESTfulBlogApp> node app.js
SERVER IS RUNNING!
(node:22323) UnhandledPromiseRejectionWarning: MongoError: there are no users authenticated
at Connection.<anonymous> (/RESTful Routing/RESTfulBlogApp/node_modules/mongodb/lib/core/connection/pool.js:454:61)
at Connection.emit (events.js:198:13)
at processMessage (/RESTful Routing/RESTfulBlogApp/node_modules/mongodb/lib/core/connection/connection.js:368:10)
at Socket.<anonymous> (/RESTful Routing/RESTfulBlogApp/node_modules/mongodb/lib/core/connection/connection.js:537:15)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:22323) 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(). (rejection id: 1)
(node:22323) [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.Note: I following this to install MongoDB in openSUSE.