0

I have json data in $scope variable and i want to use that $scope variable inside my backend app.js node file.

This is my backend file 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);
    });
})

This is my controller file:

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

I want to get $scope.excels data into my backend to bulkcreate into databases.

5
  • Describe the problem. Tell us what the expected behavior should be. Tell us what the exact wording of the error message is, and which line of code is producing it. Put a brief summary of the problem in the title of your question. Commented Aug 29, 2017 at 7:24
  • i have data in $scope.excels so i want to post that data my backend Commented Aug 29, 2017 at 7:26
  • So what is the problem? Commented Aug 29, 2017 at 7:27
  • You pass the data to your backend in the upload request. Do the similar thing for the uploadlast. You can refer the docs here: docs.angularjs.org/api/ng/service/$http#post. Commented Aug 29, 2017 at 7:30
  • @georgeawg i am not getting "excels" values in my backend Commented Aug 29, 2017 at 7:36

1 Answer 1

1

You can pass any data with a post request as the second parameter of $http.post(). So you can do something like:

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

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

And in your backend, you can access it like:

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);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.