2017-08-29 5 views
0

$ scope変数にjsonデータがあり、backend app.jsノードファイル内でその$ scope変数を使用したいとします。

これは私のバックエンドファイルapp.jsです:

app.post('/upload', upload.single('file'), function(req, res) { 

    var XLSX = require('xlsx'); 
    var workbook = XLSX.readFile('./uploads/' + req.file.filename); 
    var sheet_name_list = workbook.SheetNames; 
    var data = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]); 
    //var values = []; 
    console.log(data); 
    return res.status(200).send(data); 
}); 

app.post('/api/uploadlast',api.addNewContact, function(req,res){ 

    Contact.bulkCreate(excels).then(function(users) { 
     return res.status(200).send(users); 
    }).catch(Sequelize.ValidationError, function(err) { 
     return res.status(422).send(err.errors[0].message); 
    }).catch(function(err) { 
     return res.status(400).send(err.message); 
    }); 
}) 

これは私のコントローラファイルである:私はデータベースにbulkcreateために私のバックエンドに$ scope.excelsデータを取得したい

$scope.uploadFile = function() { 
      var file = $scope.myFile; 
      var uploadUrl = "/upload"; 
      var fd = new FormData(); 
      fd.append('file', file); 

      $http.post(uploadUrl, fd, { 
        transformRequest: angular.identity, 
        headers: { 
         'Content-Type': undefined 
        } 
       }) 
       .then(function(response) { 
        //$state.reload(); 
        $scope.excels = response.data; 
        console.log("success!!"); 
       }) 
       .catch(function() { 
        console.log("error!!"); 
       }); 

     } 

     $scope.uploadLast = function() { 
      $http.post('/api/uploadlast').then(function(response) { 
       $state.reload(); 
      }); 
     } 
    }) 

+0

**問題を説明して**予想される動作はどうあるべきかを教えてください。エラーメッセージの正確な表現と、それをどのコード行で生成しているかを教えてください。質問のタイトルに問題の概要を記入してください。 – georgeawg

+0

私は$ scope.excelsにデータを持っていますので、そのデータを私のバックエンドに投稿します –

+0

それで問題は何ですか? – georgeawg

答えて

1

投稿要求のあるデータは、第2パラメータ$http.post()として渡すことができます。だからあなたのようなものを行うことができます。

$scope.uploadLast = function() { 
    var data = { 
     excels: $scope.excels 
    }; 

    $http.post('/api/uploadlast', data).then(function(response) { 
     $state.reload(); 
    }); 
} 

そして、あなたのバックエンドでは、あなたが好きそれにアクセスすることができます。

app.post('/api/uploadlast',api.addNewContact, function(req, res){ 

    var data = req.body.excels; 


    Contact.bulkCreate(data).then(function(users) { 
     return res.status(200).send(users); 
    }).catch(Sequelize.ValidationError, function(err) { 
     return res.status(422).send(err.errors[0].message); 
    }).catch(function(err) { 
     return res.status(400).send(err.message); 
    }); 
}); 
関連する問題