gixen
gixen

Reputation: 309

MobileFirst Platform 8 Cordova: JSONStore issues on Android

I'm having some issues with JSONStore on Android. It works fine using mfpdev app preview.

Intialization code (runs successfully):

let collections = {
  workorders: {
    searchFields: {id: 'integer'}
  }
}

WL.JSONStore.init(collections).then((success) => {
  console.log("Jsonstore init success");
},
(failure) => {
  console.log("Jsonstore init failed", failure);      
});

Put code (doesn' work, see error logs below):

 put(data) : void {
console.log("JSONStore put function called: " + JSON.stringify(data));

if (!Array.isArray(data))
{
  console.log("data is not array");
  data = this.json2array(data);
}

if (Array.isArray(data)){
  console.log("data is array");
}

let collectionName = "workorders";
let options = {
  replaceCriteria: ['id'],
  addNew: true,
  markDirty: false
};

WL.JSONStore.get(collectionName).change(data, options).then((success)=> {
  console.log("JSONStore put success");
}, 
(failure) => {
  console.log("JSONstore put failed: " + JSON.stringify(failure), failure);      
});      

}

Errors from android logcat:

03-24 15:29:36.656 8362 8362 I chromium: [INFO:CONSOLE(40796)] "JSONStore put function called: {"enddate":"2017-03-03","description":"Test work order 0","id":1,"ts":"Ban","status":"In progress"}", source: file:///android_asset/www/build/main.js (40796) 03-24 15:29:36.656 8362 8362 D SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 40798 : data is not array 03-24 15:29:36.656 8362 8362 I chromium: [INFO:CONSOLE(40798)] "data is not array", source: file:///android_asset/www/build/main.js (40798) 03-24 15:29:36.657 8362 8362 D SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 40802 : data is array 03-24 15:29:36.657 8362 8362 I chromium: [INFO:CONSOLE(40802)] "data is array", source: file:///android_asset/www/build/main.js (40802) 03-24 15:29:36.662 8362 8362 D SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 40813 : JSONstore put failed: {"src":"store","err":10,"msg":"BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENTS","col":"workorders","usr":"jsonstore","doc":{},"res":{}} 03-24 15:29:36.662 8362 8362 I chromium: [INFO:CONSOLE(40813)] "JSONstore put failed: {"src":"store","err":10,"msg":"BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENTS","col":"workorders","usr":"jsonstore","doc":{},"res":{}}", source: file:///android_asset/www/build/main.js (40813)

The error is BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENTS, however I have in my code to make it an array if its not already an array.

Also, I can't find any documentation on the change() method on a JSONStore collection, but I found it is used in this example: https://mobilefirstplatform.ibmcloud.com/labs/developers/8.0/advancedmessenger/

Can anyone spot what the problem might be?

Upvotes: 0

Views: 138

Answers (1)

gixen
gixen

Reputation: 309

I finally found out what the issue was.

The error BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENTS seems to be somewhat misleading. It always failed if I sent a single JSONObject. After I converted it to a JSONArray using the below code, it worked:

if (!Array.isArray(data))
{
  console.log("data is not array");
  data = JSON.parse('[' + JSON.stringify(data) + ']');
  console.log("data is now an array: " + JSON.stringify(data));
}

Upvotes: 0

Related Questions