Bv202 Bv202 - 2 months ago 8
Javascript Question

Long value in object field not sent though REST

I have an object with a

Value
field. This field contains a very long string (base64). When using
console.log
, the field seems to contain the string, however, when just printing out this one field, I just get an empty string:

enter image description here

I'm trying to send this object through a REST service, but it just sends the empty string instead of the correct value.

What is going on here? I've used these kind of strings before and it worked fine, but now it looks like I'm missing something important.

To make it clear, this gets the above output:

console.log(myObject);


This prints an empty string:

console.log(myObject.Value);


EDIT:
This is how the
Value
is assigned:

for (var i = 0; i < vm.data.profileSettings.length; i++) {
(function (i) {
var setting = vm.data.settings.filter(function(setting) { return setting.Id == vm.data.profileSettings[i].Id });

if (setting[0].Type == 2 && vm.data.profileSettings[i].copiedValue && typeof(vm.data.profileSettings[i].copiedValue) == 'object') {
Upload.base64DataUrl(vm.data.profileSettings[i].copiedValue)
.then(function(response) {
var splitted = response.split(",");

vm.data.profileSettings[i].Value = splitted[1];
});

}
})(i);
}

profileDataService.saveProfileSettings(vm.data.selectedProfile.Id, vm.data.profileSettings)
.then(function(response) {
vm.success = true;
});

Answer

Upload() is asynchronous so you need all the Upload to complete before you send your final data since that data is dependent on responses from uploads.

You can create array of upload promises and use $q.all() to run code when all of those promises have resolved


Create empty array before the loop and push each upload promise into that array

var uploadPromises = []

Then change

Upload.base64DataUrl(vm.data.profileSettings[i].copiedValue)
                .then(function(response) {
                   // do stuff with response
                });

To

var req =  Upload.base64DataUrl(vm.data.profileSettings[i].copiedValue)
                .then(function(response) {
                   // do stuff with response
                });
 uploadPromises.push(req);

Now wrap your final request

$q.all(uploadPromises ).then(function(){    
    profileDataService.saveProfileSettings(...)
}).catch(function(err){
   // do something when any upload promise is rejected
});

Note you will need to determine strategy if any upload promise gets rejected. You can use a catch on upload promises themselves to also resolve that promise if that's what you want to do so final request will always be made