Whenever any json is entered in JSONStore it wraps the input like below: {"json":{JSON_INPUT},"_id":1}. Now if I change the id to string and try to push it to cloudant it successfully pushes it. But this is not the correct way as it will have the wrapper in it and when you will try to sync it next time with the JSONStore it will add another similar wrapper which will break your HTML pages. Now what i tried is by going one level down (var str=dirtyDocuments[i].json) and send the input(str) only, it gives the below error everytime:
[ERROR ] FWLST0105E: can upload user log file due to java.io.IOException: Unexpected character '\u0000' on line 1, column 2784.
Below is the code to sync the data back to cloudant DB:
pushToAdapter(){
let collectionName = 'activities';
let options = {};
WL.JSONStore.get(collectionName).getAllDirty(options)
.then(function (dirtyDocuments) {
for(var i=0; i < dirtyDocuments.length ; i++){
var id = dirtyDocuments[i]._id;
dirtyDocuments[i]._id=id.toString();}
var resource = new WLResourceRequest("/adapters/ServiceAdapter/pushActivities", WLResourceRequest.GET)
resource.setQueryParameter('params', dirtyDocuments)
resource.send().then((success) => {
console.log('-->JSONStore : Push document success', success)
}, (failure) => {
console.log('-->JSONStore: Push document failed', failure)
})
})
}
Adapter Method:
function pushActivities(dirtyDocs) {
var path = 'employees';
var input = {
method : 'post',
returnedContentType : 'json',
path : path,
body: {
content: JSON.stringify(dirtyDocs),
contentType: 'application/json'
}
};
return MFP.Server.invokeHttp(input);
}
Please let me know if i can help with any other input.
NOTE: I have confirmed this by giving a sample input like this: {"name":"abc"}. But it was same, so i can ensure there was no such characters in my json input.
EDIT:
pushToAdapter(){
let collectionName = 'activities';
let options = {};
WL.JSONStore.get(collectionName).getAllDirty(options).then(function (dirtyDocs) {
for(var i=0;i<dirtyDocs.length;i++){
var id=dirtyDocs[i]._id;
id=id.toString();
dirtyDocs[i]._id=id;
}
WL.Client.invokeProcedure({
adapter : 'ServiceAdapter',
procedure : 'pushActivities',
parameters : dirtyDocs // If i use say dirtyDocs[0].json it fails.
}).then(
(success)=>{console.log('success',success)},
(failure)=>{console.log('failure',failure)})}).then(function (responseFromAdapter) {
})
}
Below is the updated pushToAdapter() method that worked for me.
pushToAdapter(){
console.log("Inside push to adapter");
let collectionName = 'activities';
let options = {};
WL.JSONStore.get(collectionName).getAllDirty(options).then(function (dirtyDocs) {
var temp=[dirtyDocs[0].json]; //Here its only for one element.
WL.Client.invokeProcedure({
adapter : 'FieldsServiceAdapter',
procedure : 'pushActivities',
parameters : temp
}).then(
(success)=>{console.log('success',success)},
(failure)=>{console.log('failure',failure)})}).then(function (responseFromAdapter) {
console.log('Response from adapter',responseFromAdapter)
})
}
So the change is basically it expects an array, and I was just sending a JSON string. May be a very silly mistake to commit.
NOTE: The error mentioned above had nothing to do with this, this was a parsing error which i found out later. The above error may be because of some log files that could not be uploaded to the server.