0

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) {
})
}
Shepherd
  • 320
  • 2
  • 16
  • Can you share the code snippets or the full project here - code speaks louder than words – Srik Nov 29 '17 at 13:21
  • Few of the snippets i have added in this question. https://stackoverflow.com/questions/47489647/how-to-avoid-duplicate-entries-in-ibm-jsonstore. For now i have found out a workaround i have pushed the data as its in jsonstore but when reloading or refreshing the data i just take the payload which is inside "json" tags. I realise this shouldnt be the solution, may be a very silly workaround. – Shepherd Nov 29 '17 at 18:10
  • You are passing all of your JSON as a query parameter which is a bad idea - some clients have a limit on the size of query parameters. Instead send the JSON as part of the body. While sending the JSON as a query parameter, ensure that the data is properly URL encoded. – Srik Nov 30 '17 at 11:01
  • How should we deal with the wrapper ({"json":{"data":""},"_id":""}) that is put over the real data. When I invoke getdirtydocs it comes with this wrapper, now if I pass this enitre thing it goes fine, but it is having a different _id it will create another record, and even the strucutre is different from the real data. If I go inside the object returned and send only the data part it again gives the same error (as mentioned above). And I have updated the code a bit, i am no more sending it in query params, I have updated my question. Sorry if this is annoying, I am very new to this. – Shepherd Dec 01 '17 at 06:24

1 Answers1

0

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.

Shepherd
  • 320
  • 2
  • 16