【问题标题】:how to return json data in after save using node js mongoose使用node js mongoose保存后如何返回json数据
【发布时间】:2017-05-07 15:53:41
【问题描述】:

使用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'});
          }
        });
      }
    }); 

res.json中需要返回与文档网的_id相同的文档名和密码

【问题讨论】:

  • 你也在用 express 吗?
  • 是的,我正在使用 Express @se

标签: javascript json node.js mongodb mongoose


【解决方案1】:

根据您的要求,您希望通过 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"});
        }
    });

【讨论】:

  • 保存后会得到文件id 如何在那个@Shekhar 中得到那个id
  • 你希望 mongoid 正确存储了这个名字和密码,然后使用 id: result._id
  • 你定义了一个集合名称 var newUser = req.Collection;在您的 db.js 文件中类似
  • 是的,我在 db 中定义了
  • res.json({status: 1, name: name, password: password,id:result._id, message: "成功创建用户"});替换此行。希望它有效
【解决方案2】:

我认为您可以使用 .get() 方法和 /path/:id 作为第一个参数。像这样:

apiRoutes.get('/doctor/:id', function(req, res){
 // your code goes here
});

因此,您可以从客户端发送您的 get 请求,例如 /doctor/65431 (id)

更多关于 express .get 方法的信息here

【讨论】:

  • 我不想在单独的 get 方法中我想要在响应中的 post 方法本身
【解决方案3】:

试试这个

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

【讨论】:

  • Getting error dude'' res.json({success: true, msg : 'Successfull created user' :newUser}); SyntaxError: Unexpected token :
  • 删除 'data':newUser 并尝试
猜你喜欢
  • 2014-04-05
  • 1970-01-01
  • 2021-01-21
  • 2021-06-29
  • 2020-07-01
  • 2017-05-19
  • 2020-04-22
  • 2016-10-23
  • 1970-01-01
相关资源
最近更新 更多