Coffee Bean Coffee Bean - 6 months ago 17
AngularJS Question

How to update the user document nested field in Baasbox using webservice call

Kindly help me in updating the user document nested field.
I have a user document having visibleByTheUser field having avatarUrl, name, email as the fields.
I want to update only the avatarUrl field when profile pic is uploaded.
When i try to update it is overriding the complete object.
For reference find my service code below.

var headers = {
'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
'contentType' : 'application/json',
'X-BAASBOX-APPCODE' : baasboxAppCode,
'X-BB-SESSION' : window.localStorage.getItem("userToken")
};
//TODO: Change the updation format.
//Below format is overriding the complete object.
var data = {
"visibleByTheUser": {
"avatarUrl" : updatedAvatarUrl
},
};
url = serverUrl+ '/me';
var promise = $http({
method: "PUT",
url : url,
headers : headers,
data: data
});
promise.then(function(data){
console.log("success data in ProfileUpdation is:"+ JSON.stringify(data));

});
promise.error(function(error) {
console.log("error in ProfileUpdation is:",JSON.stringify(error));
});
return promise;

Answer

I figured out this answer. Rather than updating a specific field of nested objects is not possible in Baasbox it's better to update the whole field having the nested objects in it.

Here is the code I'm using for this

avatrUrlUpdation : function(visibleByTheUserforUpdation){
        /*console.log("entered into avatarUrlUpdation");*/
        var headers = {
                'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
                'contentType' : 'application/json',
                'X-BAASBOX-APPCODE' : baasboxAppCode,
                'X-BB-SESSION' :  window.localStorage.getItem("userToken")
        };
        //TODO: Change the updation format.
        //Below format is overriding the complete object.
        var data = {
                "visibleByTheUser" : visibleByTheUserforUpdation
        };
        url = serverUrl+ '/me';
        var promise = $http({
            method: "PUT",
            url : url,
            headers : headers,
            data: data
        });
        promise.then(function(data){
            console.log(data);
        });
        promise.error(function(error) {
            console.log("error in ProfileUpdation is:",JSON.stringify(error));
        });
        return promise;
    }