Reputation: 407
Using save function inserting the data using post method, in the same method need to get same json data what we inserted document with an id
apiRoutes.post('/doctor', function(req, res){
if(!req.body.Name || !req.body.password){
res.json({success: false, msg: 'please pass the username and password'});
}else{
var newUser = new Doctor({
Name:req.body.Name,
password : req.body.password,
});
newUser.save(function(err){
if(err){
res.json({success: false, msg :'username alredy existes'});
}else{
res.json({success: true, msg : 'Successfull created user'});
}
});
}
});
In the res.json need to return the same document name and password with _id of the documnet
Upvotes: 2
Views: 1829
Reputation: 1674
According to your requirement you want to enter name and password in db via a POST method. Then you can simple do this.
apiRoutes.post('/doctor', function (req, res) {
var newUser = req.Collection;
var name = req.body.Name;
var password = req.body.password;
var record = new newUser({
name: name,
password: password,
});
if (name && password) {
record.save(function (err, result) {
if (err) {
res.json({status: 0, message:" username alredy existes"})
} else {
res.json({status: 1, name: name, password: password, message: " Successfull created user"});
}
})
} else {
res.json({status: 0, msg: "Invalid Fields"});
}
});
Upvotes: 2
Reputation: 1802
try this
apiRoutes.post('/doctor', function(req, res){
if(!req.body.Name || !req.body.password){
res.json({success: false, msg: 'please pass the username and password'});
}else{
var newUser = new Doctor({
Name:req.body.Name,
password : req.body.password,
});
newUser.save(function(err){
if(err){
res.send({'success': 'false', 'msg' :'username alredy existes'});
}else{
res.send({'success': 'true', 'msg' : 'Successfull created user','data':newUser});
}
});
}
});
Upvotes: 0
Reputation: 724
I think you can use the .get()
method with the /path/:id
as the first param. something like this:
apiRoutes.get('/doctor/:id', function(req, res){
// your code goes here
});
So, from the client side you can send your get request with something like this /doctor/65431
(id)
More info on the express .get method here
Upvotes: 0