【问题标题】:POST http://localhost:3000/upload 500 (Internal Server Error)POST http://localhost:3000/upload 500(内部服务器错误)
【发布时间】:2017-08-17 09:07:13
【问题描述】:

当我将数据从客户端 (angular.js) 发送到服务器端 (node.js) 时出现错误。 我创建了一个表单,用户插入数据然后我的控制器获取数据并将其发送到服务器以将数据保存在猫鼬和 s3(亚马逊)中。 我必须说它工作正常 - 我的意思是我可以保存我需要的所有信息,我可以在我的数据库中看到它,我可以在 s3 上看到图像

但是,我收到一个错误:POST http://localhost:3000/upload 500 (Internal Server Error)

我的 html 表单:

<html ng-app="mymed">
 <head>
 <title>insert</title>
 </head>
 <body ng-controller="tryController">
 <main>
 <body>
        <form class="form-group" enctype="multipart/form-data" method="POST" action="http://localhost:3000/upload">
            <label for="inputEmail3" class="col-sm-2 control-label">Title:</label>
            <input class="form-control" type="text" placeholder="Title" ng-model="Title" name="Title" required></input>
          </div>
          <div class="col-sm-10">
            <br>
            <input type="file" name="file" accept="image/*" ng-modle="file"></input>
            </div>
          </div>
           <input type="submit" class="btn btn-default" name="send" ng-click="createInsert()"></input>
        </form>
.....
<script src="js/appController.js"></script>

我的控制器:

mymedical.controller('tryController',['$scope','$http','$cookies', function($scope,$http,$cookies){

        $scope.createInsert = function(){     
                var data = {};    
                data.Title = $scope.Title;
                data.file = $scope.file;      
              $http.post('http://localhost:3000/upload', JSON.stringify(data)).then() //callback
        }
}]);

服务器端:

exports.saveDatatry=function(request, response){

console.log(request.body);

var file = request.files.file;
    var hash = hasher();

    var stream = fs.createReadStream(file.path)
    var mimetype = mime.lookup(file.path);
    console.log(stream);
    var req;

    if (mimetype.localeCompare('image/jpeg')
        || mimetype.localeCompare('image/pjpeg')
        || mimetype.localeCompare('image/png')
        || mimetype.localeCompare('image/gif')) {

        req = client.putStream(stream, hash+'.png',
            {
                'Content-Type': mimetype,
                'Cache-Control': 'max-age=604800',
                'x-amz-acl': 'public-read',
                'Content-Length': file.size
            },
            function(err, result) {
             var savePerTry = new personal({
                  Title: request.body.Title,
                  file: req.url
              });
              savePerTry.save(function(error, result) {
                if (error) {
                  console.error(error);
                } else {
                  console.log("saved!");
                  response.redirect('http://localhost:8080/tryinsert.html');
                };
              })
          });
       } else {
            console.log(err);
        }

}

我需要做什么? 谢谢,

【问题讨论】:

  • 你有节点日志吗? 500 是一般错误
  • “我需要做什么?” – 首先,当然当然是:检查错误日志。
  • 这就是我得到的所有错误 - 在节点端它工作正常
  • “在节点端它工作正常” - 不,不是,否则节点不会告诉你有一个内部 服务器 错误。
  • 好的,如何检查节点端的日志错误?@CBroe

标签: javascript angularjs node.js html amazon-s3


【解决方案1】:

这是你的问题:

        function(err, result) {
          var savePerTry = new personal({
              Title: request.body.Title,
              file: req.url
          });

每次你写这样的东西:

        function(err, result) {

那么下一行应该是:

            if (err) {
              // ...
            }

如果您不处理错误,您将收到 500 个内部服务器错误,您将不知道出了什么问题。

始终处理错误。

【讨论】:

  • 我收到此错误 TypeError: Cannot read property 'file' of undefined
猜你喜欢
  • 2016-07-29
  • 2023-02-14
  • 1970-01-01
  • 2022-11-07
  • 2015-09-04
  • 2021-06-24
  • 1970-01-01
  • 2023-02-20
  • 2012-03-16
相关资源
最近更新 更多