Reputation: 194
I'm currently trying to learn mongoDB, this is my first time doing it and I'm stuck.
On my index.js file, where I've all my NodeJs configurations, I made the connection to the db successfully but I'm not able to make an insert of the data of the user inputs from the HTML.
Here is the index.js file:
const path = require("path");
const MongoClient = require("mongodb").MongoClient;
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
const port = 8080;
const uri =
"mongodb+srv://mongodbURI";
const client = new MongoClient(uri, { useNewUrlParser: true });
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.post("/new", (req, res) => {
let myObj = [
{
name: req.body.usrName,
email: req.body.usrMail,
age: req.body.usrAge,
password: req.body.usrPassword
}
];
client.connect(err => {
const collection = client.db("GetForm").collection("UserData");
collection.insertOne(myObj, function(err, r) {
console.log("Added a user");
res.redirect("/");
});
client.close();
});
});
app.listen(port, () => {
console.log(`Server Runing On Port ${port}`);
});
And here my index.html form:
<form
action="/new"
method="POST"
enctype="application/x-www-form-urlencoded"
>
<label for="usrName">Name: </label>
<input type="text" name="usrName" id="usrName" />
<label for="usrMail">Email: </label>
<input type="email" name="usrMail" id="usrMail" />
<label for="usrAge">Age: </label>
<input type="number" name="usrAge" id="usrAge" />
<label for="usrPassword">Password: </label>
<input type="password" name="usrPassword" id="usrPassword" />
<button type="submit">Add</button>
</form>
I'll be glad if someone could help me!
Upvotes: 0
Views: 947
Reputation: 17925
Your actual code has few issues :
Working code :
app.post("/new", (req, res) => {
let myObj = {
name: req.body.usrName,
email: req.body.usrMail,
age: req.body.usrAge,
password: req.body.usrPassword
};
client.connect((err, db) => {
if (err) {
console.log("cannot connect db" + err);
return;
}
console.log("DataBase connection made successfully");
const collection = db.db().collection("UserData");
collection.insertOne(myObj, function (err, r) {
if (err) {
console.log("cannot add obj");
return;
}
console.log("Added a user");
res.redirect("/");
});
client.close();
});
});
Issues with the code :
const path = require("path");
const MongoClient = require("mongodb").MongoClient;
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
const port = 8080;
const uri =
"mongodb+srv://mongodbURI";
const client = new MongoClient(uri, { useNewUrlParser: true });
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.post("/new", (req, res) => {
let myObj = [
{
name: req.body.usrName,
email: req.body.usrMail,
age: req.body.usrAge,
password: req.body.usrPassword
}
];
client.connect(err => { // You've missing err check here, you need to return if there is an error.
const collection = client.db("GetForm").collection("UserData");
/** From above line db(GetForm) is not needed if you're connecting to GetForm, your url directly connects to database :: GetForm as it ends with /GetForm.
Also other major issue is MongoDB driver would give you client on `client.connect` function, So to access database you need to do client.db() */
collection.insertOne(myObj, function(err, r) {
console.log("Added a user");
res.redirect("/");
});
client.close();
});
});
app.listen(port, () => {
console.log(`Server Runing On Port ${port}`);
});
Upvotes: 1
Reputation: 194
At the end I solved it with the help of @srinivasy and @tpikachu.
I changed the app.post
method from this:
app.post("/new", (req, res) => {
let myObj = [
{
name: req.body.usrName,
email: req.body.usrMail,
age: req.body.usrAge,
password: req.body.usrPassword
}
];
client.connect(err => {
const collection = client.db("GetForm").collection("UserData");
collection.insertOne(myObj, function(err, r) {
console.log("Added a user");
res.redirect("/");
});
client.close();
});
});
To this:
app.post("/new", (req, res) => {
let myObj = {
name: req.body.usrName,
email: req.body.usrMail,
age: req.body.usrAge,
password: req.body.usrPassword
};
client.connect((err, db) => {
if (err) {
console.log("cannot connect db" + err);
return;
}
console.log("DataBase connection made successfully");
const collection = db.db().collection("UserData");
collection.insertOne(myObj, function(err, r) {
if (err) {
console.log("cannot add obj");
return;
}
console.log("Added a user");
res.redirect("/");
});
client.close();
});
});
And luckily now it's working fine! Thanks for the help guys!
Upvotes: 0