2017-08-29 52 views
0

我有$範圍變量的json數據,我想在我的後端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); 
    }); 
}) 

這是我的控制文件:

$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(); 
      }); 
     } 
    }) 

我想$ scope.excels數據到我的後端bulkcreate到數據庫中。

+0

**描述問題**告訴我們預期的行爲應該是什麼。告訴我們錯誤信息的確切用詞是什麼,以及哪一行代碼正在生成它。在問題的標題中簡要概述問題。 – georgeawg

+0

我有$ scope.excels中的數據,所以我想發佈數據我的後端 –

+0

那麼問題是什麼? – georgeawg

回答

1

您可以將任何數據與發佈請求作爲第二個參數$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); 
    }); 
}); 
相關問題