2

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

2
  • Are you using express as well? Commented Dec 22, 2016 at 5:16
  • Yes i am using Express @se Commented Dec 22, 2016 at 5:19

3 Answers 3

2

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"});
        }
    });
Sign up to request clarification or add additional context in comments.

7 Comments

after saving will get the document id how to get that id in that @Shekhar
you want mongoid for which this name and password stored right, then use id: result._id
have you define a collection name var newUser = req.Collection; similar in your db.js file
Yes i defined in db
res.json({status: 1, name: name, password: password,id:result._id, message: " Successfull created user"}); replace this line. hope it works
|
0

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

1 Comment

I don't want in separate get method i want in post method itself in respnse
0

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});
          }
        });
      }
    }); 

2 Comments

Getting error dude'' res.json({success: true, msg : 'Successfull created user' :newUser}); SyntaxError: Unexpected token :
remove 'data':newUser and try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.