Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm pretty new to this. Any help would really help!!!!!!

Basically it's a system where Angular runs through an array of files and then makes upload requests to the server. The first step that the server takes is to add the file to the database, and then to upload the file.

Angular is sending the files correctly [1,2,3,4] but when the SERVER goes to post the data to the database, I'm finding that it repeats random objects and skips other ones.

This is really confusing me.

Here is my code:

Angular:

       for (var i = 0; i < files.length; i++) {
            console.log('angular uploading file!')
            console.log(files[i].trackNumber)
          Upload.upload({
            url: '/../api/createTrack', 
            data: {file: files[i].trackFile, trackUserName: scope.username, trackNumber: files[i].trackNumber, trackName: files[i].trackName, trackAlbum: scope.albumName, trackArtist: scope.artistEditName, trackArtistImage: scope.currentArtistData.artistImage, trackAlbumTrackLength: scope.tracks.length},
                file: files[i].trackFile
            })
        }

Node.js:

createTrack.prototype.uploadFile = function(req, res) {

    var file = req.files.file;

    addWavFile = function(trackWavSource){
        read_file = fs.readFileSync(file.path)
        var params = {Bucket: 'dogatracks', Key: trackWavSource, Body: read_file, ACL:"public-read"}
        s3.putObject(params, function(err, data) {
            if (err) {
                console.log("Error uploading data: ", err)
            } else {
            etc

    addTrackToDatabase = function(artistId, albumId, trackAlbumArt, trackWavSource){
        const dateAdded = new Date()
        Track
        .create({
            trackName:req.body.trackName,
            trackNumber: req.body.trackNumber,
            trackAlbum: req.body.trackAlbum,
            ETC

Now, it works, the files are sometimes uploaded pretty well, however sometimes for some reason the files duplicate in the database... here is what the logs output for instance:

addtodatabase:
1
add File:
1
addtodatabase:
1
add File:
1
addtodatabase:
3
add File:
3
addtodatabase:
4
add File:
4

As you can see, the system generally works, but for some reason the file randomly will repeat itself! The first track was uploaded twice, and added to the database twice. This doesn't just happen to the first track. Also, sometimes all the tracks upload fine. In the Angular log, the files are always sent as 1,2,3,4 in correct order. I feel like I'm missing something about how the server/HTTP actually works.

Does this have something to do with timing? I don't get how it is that the server would miss requests or repeat a request, because Angular is clearly sending the correct information inside of the loop each time.

Any help is greatly appreciated.

share|improve this question
    
you aren't confirming the upload success (or error) in the angular code. – charlietfl 30 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.