Reputation: 29
I am developing a 3 page registration form and storing data using mongodb. But due to multiple pages, i created the collection "detail" outside of all the post requests of all the 3 pages. But Now it always stores only one data. it shows only one user with the recent details. please tell me a way to store every user details. I know that i have to make all the fields equal to NULL but when in the last i did this(first saved and then made fields equal to null), then in my database I got null values only. Please help me.
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express( );
app.use(bodyParser.urlencoded({extended:true}));
mongoose.connect("mongodb://localhost:27017/detailsdb", {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.set('useFindAndModify', false);
const detailsSchema = {
Name: String,
Divorce_id: String,
Date_of_Birth: String,
Nationality: String,
Marital_Status: String,
Highest_Education: String
};
const Detail = mongoose.model("Detail", detailsSchema);
const detail = new Detail({
Name: null,
Divorce_id: null,
Date_of_Birth: null,
Nationality: null,
Marital_Status: null,
Highest_Education: null
});
app.get("/FORM-1",function(req,res)
{
res.sendFile(__dirname+"/form1.html");
});
app.get("/FORM-2",function(req,res)
{
res.sendFile(__dirname+"/form2.html");
});
app.get("/FORM-3",function(req,res)
{
res.sendFile(__dirname+"/form3.html");
});
app.post("/FORM-1", function(req, res){
detail.Name = req.body.name;
detail.Divorce_id = req.body.id;
res.redirect("/FORM-2");
});
app.post("/FORM-2", function(req, res){
detail.Date_of_Birth = req.body.dob;
detail.Nationality = req.body.nationality;
res.redirect("/FORM-3");
});
app.post("/FORM-3", function(req, res){
detail.Marital_Status= req.body.marital_status;
detail.Highest_Education= req.body.highest_education;
detail.save();
});
app.listen(3000, function(){
console.log("server started on port 3000");
});
Upvotes: 0
Views: 120
Reputation: 132
The approach is very wrong here,
detail
is being a global object. updates coming for any request would be saved in ambigous order.FORM_1
's POST request must be saved and it's documentId _id
must be returned to the form for subsequent requests made to FORM_2
and FORM_3
to make the update and not save.null
it'll actually be saved as null, not like it'll not get updated.Suggestions:
in FORM_1
app.post("/FORM-1", function(req, res){
let detail = new Detail({
Name: req.body.name,
Divorce_id: req.body.id;,
Date_of_Birth: null,
Nationality: null,
Marital_Status: null,
Highest_Education: null
});
detail.save(function(err,savedDetail){
if(!err)
res.json({savedDetail});
})
//res.redirect("/FORM-2");
});
in FORM_2
app.post("/FORM-2", function(req, res){
let updateDetails = {
Date_of_Birth : req.body.dob,
Nationality : req.body.nationality
};
Detail.findOneAndUpdate({
_id : req.body._id
},{
$set : updateDetails
},function(err,data){
if(!err){
res.redirect("/Form-3");
}
});
//detail.Date_of_Birth = req.body.dob;
//detail.Nationality = req.body.nationality;
//res.redirect("/FORM-3");
});
in Form_3
app.post("/FORM-3", function(req, res){
let updateDetails = {
Marital_Status : req.body.marital_status,
Highest_Education : req.body.highest_education
};
//detail.Marital_Status= req.body.marital_status;
//detail.Highest_Education= req.body.highest_education;
Detail.findOneAndUpdate({
_id : req.body._id
},{
$set : updateDetails
},function(err,data){
if(!err){
res.json({updated : true});
}
});
//detail.save();
});
Upvotes: 2
Reputation: 312
The Detail must be initialized in the FORM_1
's POST request
and then sent immediately to the database using detail.save()
;
and then you must get the above created document's id and pass it to the FORM_2
and perform a update operation on the object on the next post request for FORM_2
and likewise for FORM_3
Upvotes: 0