I have installed mongoose using npm install mongoose .But when i try to run the js file its not connecting.
see my code below.
const c = require('config')
const express= require('express')
const app= express()
const port=3000
const mongoose = require('mongoose')
const url = "mongodb+srv://mongouser:mongouser#cluster0.jpdno.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
const connectdb = async () => {
await mongoose.connect(url)
console.log('db connected')
}
app.listen(port,() => console.log("listening..."))
I am not getting any errors but after listening.. nothing is showing.
I am new to this please help me.
You are on the right path. To check whether you successfully connected to database use:
mongoose.connection.on('connected', () => { console.log('Successfully connected to database' });
Also you want to return your connection so you could use it later for database manipulation. It should look like this:
const connectdb = async () => {
await mongoose.connect(url);
return mongoose.connection; // Please note that you are returning reference
}
Last notes, I believe async/await is redundant when dealing with mongoose inside Node.js and you should probably want to include some option argument on your connect method:
mongoose.connect(url, {useNewUrlParser: true, useUnifiedTopology: true} );
Now you should be good to go!
Related
https://i.imgur.com/w5quRwA.jpg
I manually created a database called "shoppingitems" on the mongodb website console. I then created a model called "products" in an Express app and connected to the database. A collection called "products" was added to the "shoppingitems" database like I expected.
I then went to add a document to the "shoppingitems.products" collection, but instead an entirely new database called "test" was created, with a products collection and my submitted document in that 'test.products" collection instead of the "shoppingitems.products" collection like I intended.
Is there something wrong with my code? I make no mention of a "test" database anywhere, so IDK why it was created in the first place.
index.js
//Express
var express = require("express");
const app = express();
app.use(express.json());
//Mongoose
const dotenv = require("dotenv");
dotenv.config();
const mongoose = require("mongoose");
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("db connection succesfull"))
.catch((err) => console.log(err));
//CORS
const cors = require("cors");
app.use(cors());
//Routes
const productRoute = require("./routes/products");
app.use("/", productRoute);
//RUN INDEX.JS
app.listen(5000, () => {
console.log("backend server is running");
});
routes/products.js
var express = require("express");
var router = express.Router();
var Product = require("../models/Products");
/* GET PRODUCTS FOR HOMEPAGE */
router.get("/", async (req, res) => {
try {
productList = await Product.find();
res.json(productList);
} catch (error) {
console.log(error);
}
});
//POST PRODUCTS TO DATABASE
router.post("/", async (request, response) => {
console.log("request.body= ", request.body);
const newProduct = new Product(request.body);
try {
const savedProduct = await newProduct.save();
response.status(201).json(savedProduct);
} catch (err) {
response.status(500).json(err);
}
});
module.exports = router;
models/Products.js
const mongoose = require("mongoose");
const ProductSchema = new mongoose.Schema({
name: { type: String },
price: { type: Number },
description: { type: String },
image: { type: String },
stripeId: { type: String },
});
module.exports = mongoose.model("Product", ProductSchema);
Am I missing something? I don't see anything in the code that would be causing this and creating a "test" database. I've only used Mongo once or twice before though so I'm not exactly an expert. Can anybody here see what I'm doing wrong?
I'll post any additional code or information that you think is necessary to solving this. Just tell me what else you need to see.
test is the default database name used if you don't specify one. I also notice that nowhere in the code is a shoppingsitems database mentioned.
The connection string could contain the database name, but in this code that is taken from an environment variable.
I found a nice npm package by the name of Express Async Errors which according to the documentation, it's really nice to use.
However, if I implement it, the server will crash.
Here is my Route handler code
Controller
const { Genre } = require("../models");
const { StatusCodes } = require("http-status-codes");
const getGenre = async (req, res) => {
const genre = await Genre.findById({ _id: req.params.id });
if (!genre) {
return res.status(StatusCodes.BAD_REQUEST).json({
message: "The genre with the given ID was not found.",
});
}
res.status(StatusCodes.OK).json({ status: "success", data: genre });
};
*router*
const express = require("express");
const { authenticate, admin } = require("../middleware");
const router = express.Router();
const { schemaValidator } = require("../middleware");
const validateRequest = schemaValidator(true);
const { genres } = require("../controllers");
const { getAllGenres, getGenre, createGenre, updateGenre, deleteGenre } =
genres;
.route("/genres")
.get(getAllGenres)
Main Application Entry
require("express-async-errors");
//Routers
const routers = require("./router");
const connectDB = require("./DB/connect");
const express = require("express");
const app = express();
app.use(config.get("URI"), routers);
app.use(notFoundMiddleware);
const start = async () => {
const port = process.env.PORT || 3000;
const connectionString = config.get("mongoDB.connString");
await connectDB(connectionString)
.then(() => DBdebug(`Connected to MongoDB: ${connectionString}`))
.catch(() => console.log("MongoDB connection failure"));
app.listen(port, () => debug(`Listening on port ${port}...`));
};
start();
Above code is imported into index.js together with express-async-errors.
According to the document, if I create an error, express-async-errors has to handle this without crashing the application. My question is what I'm doind wrong???
I shut down the Mongo-driver just to create a scenario that the server is down with a status 503.
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Function.Model.$wrapCallback (/Volumes/Seagate/lib/model.js:5087:32)
at /Volumes/Seagate/lib/query.js:4510:21
at /Volumes/Seagate/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
From previous event:
at promiseOrCallback (/Volumes/Seagate/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at model.Query.exec (/Volumes/Seagate/node_modules/mongoose/lib/query.js:4509:10)
at model.Query.Query.then (/Volumes/Seagate/node_modules/mongoose/lib/query.js:4592:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Instead of above error, I should see "Something went wrong" as the default message in Postman without crashing the application.
Can someone point me to the right direction?
NB: The link which I had used us is https://www.npmjs.com/package/express-async-errors
Thank you very much in advanced.
After two days of researching my problem, I finally convinced my self that the problem was me and found a solution for this particular matter.
I have created an ErrorHandlerMiddleware and in the particular middleware I check if the error is an instance of MongooseError object, if this is the case, I just send for now an custom message.
See code below:
const { StatusCodes } = require("http-status-codes");
const { CustomApiError } = require("../errors");
const Mongoose = require("mongoose");
const errorHandlerMiddleware = (err, req, res, next) => {
console.log("errorMiddleWare");
if (err instanceof CustomApiError) {
return res.status(err.statusCode).json({ message: err.message });
}
if (err instanceof Mongoose.Error) {
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
message: "There is a technical problem, please try again later",
});
}
};
module.exports = { errorHandlerMiddleware };
In main application entry, I just pass errorHandlerMiddleware as a argument reference in app.use.
See code below
require("express-async-errors");
//Routers
const routers = require("./router");
const connectDB = require("./DB/connect");
const express = require("express");
const app = express();
app.use(config.get("URI"), routers);
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const start = async () => {
const port = process.env.PORT || 3000;
const connectionString = config.get("mongoDB.connString");
await connectDB(connectionString)
.then(() => DBdebug(`Connected to MongoDB: ${connectionString}`))
.catch(() => console.log("MongoDB connection failure"));
app.listen(port, () => debug(`Listening on port ${port}...`));
};
start();
And this is the final result in PostMan:
If there are any comments regarding this solution, feel free to do this.
By comments and can learn more!!!
Thank you in advanced and keep coding!!!!
While trying to build user authentification using mongodb and express I came across an error. The error basically happens every time I click submit on the website. That's the first problem, the second is after I click submit the page just keeps loading and never changes as if its stuck or trapped in an infinite loop. I tried deleting everything in the database and starting over but that didn't work. A little advice would be appreciated.
The error:
app: authRouter {
app: authRouter acknowledged: true,
app: authRouter insertedId: new ObjectId("613b5afd2139aeac87bd9682")
app: authRouter } +0ms
app: authRouter TypeError: Cannot read property '0' of undefined
app: authRouter at addUser (C:\Users\Yanki XXIV\Desktop\pluralsight\src\routers\authRouter.js:24:30)
app: authRouter at processTicksAndRejections (internal/process/task_queues.js:95:5) +19ms
authRouter.js:
const express = require('express');
const debug = require('debug')('app: authRouter');
const { MongoClient } = require('mongodb');
const authRouter = express.Router();
authRouter.route('/signUp').post((req, res) => {
const {username, password} = req.body;
const url =
'mongodb+srv://Yoshi:Yumcmaster1#cluster0.atic5.mongodb.net?retryWrites=true&w=majority'
const dbName = 'testdb';
(async function addUser(){
let client
try {
let client = await MongoClient.connect(url);
const db = client.db(dbName);
const user = {username, password};
const results = await db.collection('users').insertOne(user);
debug(results);
req.login(results.ops[0], ()=> {
res.redirect('/auth/profile');
});
} catch (error) {
debug(error)
}
client.close();
}());
});
authRouter.route('/profile').get((req, res) => {
res.json(req.user);
})
module.exports = authRouter;
I certainly don't know what your trying to do because I'm not so good at mongodb but I found a rather intresting thing in passport js docs and stackoverflow
Note: passport.authenticate() middleware invokes req.login() automatically. This function is primarily used when users sign up, during which req.login() can be invoked to automatically log in the newly registered user.
Check version of MongoDB Node.js Driver with command npm list mongodb. If it is version 4 then you can not access result.ops. In version 4 insertOne returns insertOneResult that have only 2 properties: acknowledged and insertedId.
NodeJS + MongoDB: insertOne() - get the inserted document from result.ops
http://www.passportjs.org/docs/login/
I'm kinda new to module creation and was wondering about module.exports and waiting for async functions (like a mongo connect function for example) to complete and exporting the result. The variables get properly defined using async/await in the module, but when trying to log them by requiring the module, they show up as undefined. If someone could point me in the right direction, that'd be great. Here's the code I've got so far:
// module.js
const MongoClient = require('mongodb').MongoClient
const mongo_host = '127.0.0.1'
const mongo_db = 'test'
const mongo_port = '27017';
(async module => {
var client, db
var url = `mongodb://${mongo_host}:${mongo_port}/${mongo_db}`
try {
// Use connect method to connect to the Server
client = await MongoClient.connect(url, {
useNewUrlParser: true
})
db = client.db(mongo_db)
} catch (err) {
console.error(err)
} finally {
// Exporting mongo just to test things
console.log(client) // Just to test things I tried logging the client here and it works. It doesn't show 'undefined' like test.js does when trying to console.log it from there
module.exports = {
client,
db
}
}
})(module)
And here's the js that requires the module
// test.js
const {client} = require('./module')
console.log(client) // Logs 'undefined'
I'm fairly familiar with js and am still actively learning and looking into things like async/await and like features, but yeah... I can't really figure that one out
You have to export synchronously, so its impossible to export client and db directly. However you could export a Promise that resolves to client and db:
module.exports = (async function() {
const client = await MongoClient.connect(url, {
useNewUrlParser: true
});
const db = client.db(mongo_db);
return { client, db };
})();
So then you can import it as:
const {client, db} = await require("yourmodule");
(that has to be in an async function itself)
PS: console.error(err) is not a proper error handler, if you cant handle the error just crash
the solution provided above by #Jonas Wilms is working but requires to call requires in an async function each time we want to reuse the connection. an alternative way is to use a callback function to return the mongoDB client object.
mongo.js:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<user>:<pwd>#<host and port>?retryWrites=true";
const mongoClient = async function(cb) {
const client = await MongoClient.connect(uri, {
useNewUrlParser: true
});
cb(client);
};
module.exports = {mongoClient}
then we can use mongoClient method in a diffrent file(express route or any other js file).
app.js:
var client;
const mongo = require('path to mongo.js');
mongo.mongoClient((connection) => {
client = connection;
});
//declare express app and listen....
//simple post reuest to store a student..
app.post('/', async (req, res, next) => {
const newStudent = {
name: req.body.name,
description: req.body.description,
studentId: req.body.studetId,
image: req.body.image
};
try
{
await client.db('university').collection('students').insertOne({newStudent});
}
catch(err)
{
console.log(err);
return res.status(500).json({ error: err});
}
return res.status(201).json({ message: 'Student added'});
};
I am needing to reuse a MongoDB connection in multiple different files for my Electron app but am having an issue that I don't understand. This led me to create a module to handle this.
db.js
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config();
let db;
function connect () {
return MongoClient.connect(`mongodb://${process.env.DB_HOST}?authSource=${process.env.DB_NAME}`, {
auth: {
user: process.env.DB_USER,
password: process.env.DB_PASS
},
useNewUrlParser: true
}).then(client => {
db = client.db(process.env.DB_NAME);
}).catch(error => {
console.error(error);
});
}
function getDB () {
return db;
}
module.exports = { connect, getDB };
I them required this in my main file which is one of the files it is needed in.
app.js
const mongoDB = require(path.resolve(`${__dirname}/assets/js/db`));
let db;
app.on('ready', async () => {
await mongoDB.connect();
db = mongoDB.getDB();
setTimeout(createWindow, 0);
});
This works and I can use it in the createWindow function. a few seconds after, the app load the index.html file along with the index.js file and creates the window. index.js is another file I need to use it in so I require it there as well.
index.js
const mongoDB = require(path.resolve(`${__dirname}/js/db`));
const db = mongoDB.getDB();
console.log(db);
This results in db being undefined. Isn't the first require in app.js supposed to be cached?