2017-02-13 60 views
1

我正在创建一个RESTful API。为什么不能将Restful传递到数据库中

我想用GET方法检查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方法效果很好,不是问题。

+0

你是不是使用后的路线在同一台服务器?该xmlhttprequest发臭,它不应该在这里,而是使用'request'或类似的。你为什么这样做:第一个地方?只需在dba本地创建用户即可。 – Zlatko

+0

@Zlatko感谢您的评论。我刚开始学习RESTful API,不太好。我想先检查姓氏是否存在,如果没有找到,则创建为dba。你能给我提供一个如何使用'request'的例子吗?谢谢 – Wei

+0

有多条路线。查看MongoDB的upsert(更新现有的文档,或创建一个新的文档,如果它不存在)。但是,如果这不起作用,您就已经在这里建立起来,您没有用户 - 所以马上创建一个。 – Zlatko

回答

1

让我修改了一下你的代码,并添加注释为指针:

// 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 
     }); 
     }); 
    }); 
    }) 
+0

非常感谢你提供这样一个很好的例子,Zlatko! – Wei

相关问题