Hello, I'm a newcomer to the world of Javascript and MobileFirst. I am currently attempting to retrieve and display a list of JSON values:
[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]
However, when trying to output these values in the DOM, it fails and shows this error message:
03-27 04:29:19.035: E/NONE(3093): Uncaught Exception: Uncaught SyntaxError: Unexpected token o at (compiled_code):311
Below is the function causing the issue:
main.js:
function add(){
var collectionName = 'people';
// Object that defines all the collections.
var collections = {
// Object that defines the 'people' collection.
people : {
// Object that defines the Search Fields for the 'people' collection.
searchFields : {name: 'string', age: 'integer'}
}
};
// Optional options object.
var options = {
};
WL.JSONStore.init(collections, options)
.then(function () {
// Data to add, which could be obtained from a network call.
var data = [{name: 'carlos', age: 10}];
// Optional options for add.
var addOptions = {
// Mark data as dirty (true = yes, false = no), default true.
markDirty: true
};
// Get an accessor to the people collection and add data.
return WL.JSONStore.get(collectionName).add(data, addOptions);
})
.then(function (numberOfDocumentsAdded) {
alert("Data successfully added.");
})
.fail(function (errorObject) {
alert("Data failed to be added.");
});
}
function display(){
var collectionName = 'people';
var options = {
};
WL.JSONStore.get(collectionName)
.findAll()
.then(function (arrayResults) {
var arrayResult = '{"files":' + arrayResults + '}';
alert(arrayResults.length);
var jsonData = JSON.parse(arrayResult);
alert (jsonData.files.length);
alert(jsonData);
var full_list = "";
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
full_list = full_list + file.json.name + " " + file._id + " is " + file.json.age + '<br />';
$("#demo").append(full_list);
}
})
.fail(function (errorObject) {
});
}
index.html
<a href="#" onclick="display();" >click here to display the data</a>
<br />
<a href="#" onclick="deldocument();" >click here to delete the data</a>
<p id="demo"></p> <br />
<a href="#" onclick="destroy();" >click here to destroy the data</a>
<p id="demo"></p> <br />
I would greatly appreciate any assistance with this issue. Thank you all so much.