Hi All,
Can anyone help me, I am looking to add 10 rows into a mongodb database from a html form. I have managed to add 1 single row into the database from the form but how would I make it insert 10 and so on 100 also how do I redirect back to the page once I have inserted data into the database it currently shows a blank page with the following
'‘Check the node command prompt for time of execution’
(can this be redirected and shown on my insert.html page
I have the following
HTML - insert.html
<h1>Intro to Node and MongoDB<h1>
<form method="post" action="/addname">
Email: <input type="text" name="email" placeholder="Enter Email.." required>
Name: <input type="text" name="name" placeholder="Enter Name.." required>
Website: <input type="text" name="qebsite" placeholder="Enter Website.." required>
<input type="submit" value="Add Name">
</form>
<footer>Copyright </footer>
app.js
app.post("/addname", (req, res) => {
var myData = new User(req.body);
console.time("item saved to database");
myData.save()
.then(item => {
res.send("Check the Node Command Prompt for the time of execution:");
console.timeEnd("item saved to database");
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
User.js
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
email: {
type: String,
},
name: {
type: String,
},
qebsite: {
type: String,
},
});
module.exports = mongoose.model('User', userSchema);
I look forward to hearing from you