如何使用node js mongoose在保存后返回json数据

时间:2021-02-28 22:57:56

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

使用save函数使用post方法插入数据,在相同的方法中,需要获得与我们使用id插入文档相同的json数据

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

在res.json中,需要使用文档网的_id返回相同的文档名和密码

3 个解决方案

#1


2  

According to your requirement you want to enter name and password in db via a POST method. Then you can simple do this.

根据您的要求,您需要通过POST方法在db中输入姓名和密码。然后你就可以这么做了。

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

#2


0  

I think you can use the .get() method with the /path/:id as the first param. something like this:

我认为您可以将.get()方法与/path/:id作为第一个参数。是这样的:

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)

所以,从客户端你可以发送你的get请求像这样/doctor/65431 (id)

More info on the express .get method here

更多关于快递的信息,获取方法在这里

#3


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

#1


2  

According to your requirement you want to enter name and password in db via a POST method. Then you can simple do this.

根据您的要求,您需要通过POST方法在db中输入姓名和密码。然后你就可以这么做了。

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

#2


0  

I think you can use the .get() method with the /path/:id as the first param. something like this:

我认为您可以将.get()方法与/path/:id作为第一个参数。是这样的:

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)

所以,从客户端你可以发送你的get请求像这样/doctor/65431 (id)

More info on the express .get method here

更多关于快递的信息,获取方法在这里

#3


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