繁体   English   中英

为什么Restful不能将body传入数据库

[英]Why can't Restful pass body into database

我正在创建一个 RESTful API。

我想使用 GET 方法来检查lastName存在。 如果能找到lastName,则返回“YES”,否则,调用POST方法创建一个输入lastName的数据。

问题是它可以创建新数据,但主体是空的。 理想情况下,它应该包含一个带有 lastName 的值,例如"lastName": "James",

{
  "_id": "58a22c3c3f07b1fc455333a5",
  "__v": 0
}

这是我的代码。

router.route("/findLastName/:id")
    .get(function(req,res){
        var response;
        mongoOp.findOne({deviceID: req.params.id}, function(err, result){
          if (err) { 
            response = {"error" : true,"message" : "Error fetching data"};
            res.json(response);
          }
          if (result) {
            response = "YES";
            res.send(response);
          } else {
            var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
            var xhr = new XMLHttpRequest();
            var POSTurl = "http://localhost:6002/users";
            var params = "lastName=" + req.params.id;

            xhr.open("POST", POSTurl, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send(params);
          }
        });
      })

PS: GET方法效果很好,不是问题。

让我修改一下您的代码并添加注释作为指针:

// changed findLastName to find-last-name. It's a common convention,
// urls need to be case insensitive. It doesn't concern lastName, as
// that's a parameter, internal to your app so it's fine.
// even better if you name the route `find-or-create` or something, to better
// reflect what you're doing.
router.route("/find-last-name/:lastName")
.get(function(req,res){
    var response;
    mongoOp.findOne({deviceID: req.params.lastName}, function(err, result){
      if (err) { 
        response = {"error" : true,"message" : "Error fetching data"};
        // Adding a `return statement here. If you don't return, you'll tell 
        // the user that there was an error, but your code continues running
        // potentially calling that res.json twice.
        // Also, since it's an internal error, it's common to tell the client
        // about it, by setting the status to 500
        return res.status(500).json(response);
      }
      if (result) {
        // turning the message to JSON instead. You started that above,
        // and for the sake of your clients (your frontend), it's 
        // better to stick to JSON. Also you can pass useful info, such as
        // _id of the document.

        // Again adding a `return` here, and then the rest of the code
        // is nested one level less. not required, but some people like that.
        response = {
          message: "Last name exists."
        };
        return res.json(response);
      }
      // Here begins the new code. I'm typing what I can infer from your code,
      // I don't know if your MongoDB driver looks like that exactly.
      mongoOp.insert({ 
        deviceId: req.params.lastName
        // add other optional properties here.
      }, function (err, response) {
        if (err) {
          var message = {
            error: true,
            message: 'Cannot save new entry.'
          }
          return res.status(500).json(message);
        }
        // if we're here, everything went ok. You can probably return
        // the _id of the given user.
        return res.json({
          message: 'New user created.',
          _id: response._id
        });
      });
    });
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM