简体   繁体   English

尝试发布时,MeanJS内部服务器错误500

[英]MeanJS Internal server error 500 when trying to post

I am getting this error in the console when I am trying to insert a new record into the database. 尝试将新记录插入数据库时​​,控制台中出现此错误。

"POST http://localhost:3000/api/task 500 (Internal Server Error)". “ POST http:// localhost:3000 / api / task 500(内部服务器错误)”。

Here's the function: 功能如下:

addTask(newTask) {
   var headers = new Headers();
   headers.append('Content-Type', 'application/json');
   return this.http.post('http://localhost:3000/api/task', JSON.stringify(newTask), { headers: headers })
     .map(res => res.json());
}

Here's the server side code for post: 这是发布的服务器端代码:

     //Save task
    router.post('/task', function() {
      var task = req.body;
      if (!task.title || !(task.isDone + '')) {
        res.status(400);
        res.json({
          "error": "Bad Data"
        });

      } else {
    db.tasks.save(task, function(err, task) {
      if (err) {
        res.send(err)

      }
      res.json(task);

    });
  }

});

Your server side route and http call are different. 您的服务器端路由和http调用是不同的。 In your http call you are looking for the route /api/task , but your router is exposing the route /task . 在您的http呼叫中,您正在寻找路由/api/task ,但是您的路由器正在公开路由/task You can either add /api to the beginning of your route, or have your client side call the /task route instead. 您可以将/api添加到路由的开头,也可以让客户端调用/task路由。

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

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