Unexpected error in parsing json - javascript

Using the tutorial given here, I am making an app to fetch json data from a URL and display it. I am using this code to call the URL and parse.
{
xtype: 'nestedlist',
title: 'Blog',
iconCls: 'star',
cls: 'blog',
displayField: 'title',
store: {
type: 'tree',
fields: ['uuid', 'display'
],
root: {
leaf: false
},
proxy: {
type: 'scripttag',
url: 'http://localhost:8081/openmrs-standalone/ws/rest/v1/location',
reader: {
type: 'json',
rootProperty: 'results'
}
},
},
},
In the console i am getting response in the form
{
"results": [
{
"uuid": "c0937f0c-1691-11df-97a5-7038c432aabf",
"display": "Chulaimbo",
"links": [
{
"uri": "http://localhost:8081/openmrs-standalone/ws/rest/v1/location/c0937f0c-1691-11df-97a5-7038c432aabf",
"rel": "self"
}
]
},
{
"uuid": "c0937d4f-1691-11df-97a5-7038c432aabf",
"display": "Mosoriot Hospital",
"links": [
{
"uri": "http://localhost:8081/openmrs-standalone/ws/rest/v1/location/c0937d4f-1691-11df-97a5-7038c432aabf",
"rel": "self"
}
]
},
{
"uuid": "8d6c993e-c2cc-11de-8d13-0010c6dffd0f",
"display": "Unknown Location",
"links": [
{
"uri": "http://localhost:8081/openmrs-standalone/ws/rest/v1/location/8d6c993e-c2cc-11de-8d13-0010c6dffd0f",
"rel": "self"
}
]
}
]
}
but its showing an error
"location" is the name of the service.

The console believes you are trying to execute the JSON as code. So after the first {, it expects instruction. Not data. Try putting parentheses around the entire thing:
{ myfield: 1, anotherfield: 2 } (ERROR)
({ myfield: 1, anotherfield: 2 }) (SUCCESS)
This will resolve the issue.

I have done it using a different method. I used proxytype 'rest' and run OpenMRS and my application on the same server and port otherwse my instance of OpenMRS wont allow cross-domain calls. The problem in this question was using type as "scripttag" which i still dont know what is the problem with it.Using "rest" resolves it.

Related

Kendo grid removes row, but does not call destory URL

I have a Kendo grid where I'm trying to add a delete feature. My datasource looks like:
var datasource = new kendo.data.DataSource({
transport: {
read: {
url: Router.action("Admin", "GetScansForMailItem", { mailItemIdnt: detailinit.data.MailItemIdnt }),
dataType: "json"
},
destroy: {
url: Router.action("Admin", "DeleteScan"),
type: "post"
}
},
model: {
id: "ScanIdnt",
fields: {
ScanIdnt: {editable: false, nullable: false}
}
},
pageSize: 5
});
I added the model part because this answer, however it made no difference.
The actual grid looks like:
.kendoGrid({
dataSource: datasource
scrollable: false,
sortable: true,
pageable: true,
editable: "inline",
columns: [{
field: "ScanIdnt",
title: "Scan ID"
}, {
field: "CreatedDate",
title: "Created",
template: "#= kendo.parseDate(CreatedDate, 'yyyy/MM/dd') #"
}, {
field: "ScanDocumentRelativePath",
title: "File Path",
template: "<a href='/CAMP/Admin/Download?scanIdnt=#= ScanIdnt #'>#= ScanDocumentRelativePath.substring(1) #</a>"
}, {
field: "ScanUserIdnt",
title: "Scanned By"
},{
command: "destroy",
title: ""
}]
});
Strangely, clicking the delete button removes the from the gird on the UI, but there is absolutely no Ajax call is made the the destroy URL. I can't seem to figure out why. Any ideas?
EDIT I'd like to point out that this grid is in fact a nested grid inside of another grid (like here) I discovered that the parent grid handles actually makes a call, but to the wrong function. For some reason, it clicking delete on a to level item calls the read function of the nested grid, however, the nested grids do nothing
Figured it out (sorta). While I think there were many issues with my code and the grid, It seems that when it came down to it, Kendo didn't like how I had my data.
In the Kendo docs related to hierarchical grids, the data for the child grid is stored in a field of the data for the parent. For example, given the following JSON:
"ParentItems": [
{
"Id": 12345 ,
"Name": "Test1",
"ChildItems": [
{"Id": 1, "Name": "Test"},
{"Id": 2, "Name": "Test"}
]
},
{
"Id": 12346 ,
"Name": "Test2",
"ChildItems": [
{"Id": 1, "Name": "Test"},
{"Id": 2, "Name": "Test"}
]
}
]
In the parent grid, each ParentItem would display it's respective ChildItems in the child grid.
On the other hand, I was pulling both data sets separately. Basically, I pulled the ParentItems like:
"ParentItems": [
{
"Id": 12345,
"Name" : "Test1"
},
{
"Id": 12346,
"Name" : "Test2"
}
]
And then made a second request to pull the child items, based on the parent's id.
"ChildItems": [
{"Id": 1, "Name": "Test", "ParentId": "12345"},
{"Id": 2, "Name": "Test", "ParentId": "12345"}
]
I was able to modify the server side code to serve the data like in the very first example and managed to get things working. The specific document that helped me out can be found here

Is there any way in nodejs to print log4js output in JSON format?

I am trying to get output of log4js in JSON format so I can easily trace it.
Is there any way that we have to set in configuration.json so output produced by log4js will be in JSON format?
Currently I am using following config.
{
"appenders": [
{
"category": "XXXXX",
"type": "dateFile",
"filename": "XXXXXXXX",
"pattern": "-from-MM-dd",
"layout": {
"type": "messagePassThrough"
}
},
{
"category": "XXXXXXXXX",
"type": "dateFile",
"filename": "XXXXXXXX",
"pattern": "-from-MM-dd",
"layout": {
"type": "messagePassThrough"
}
}
],
"levels": {
"XXXXConfig": "INFO",
"XXXXXjectConfig" : "INFO"
}
}
and I got output is in following format :
DEBUG: 1458562784032 : 2016-03-21T12:19:44.4444+00:00 : Data in request: : {
"action": "XXXXXXX",
"user": {
"username": "XXXX",
"id" : XXXX,
"pos" : XXXX
},
"version": 111
}
instead I want it in (Something like following structure) :
{"id" : "1458562784032", "time" : "2016-03-21T12:19:44.4444+00:00", "message" : "Data in request:", "data" : "{
"action": "XXXXXXX",
"user": {
"username": "XXXX",
"id" : XXXX,
"pos: : XXXX
},
"version": 111
}" }
May be there is something I missing in config.
Thanks for helping.
There are two ways to implement this issue now, you can add your own layouts or pattern format.
Below is the sample code of adding your own layouts with JSON format
const log4js = require('log4js');
log4js.addLayout('json', function(config) {
return function(logEvent) { return JSON.stringify(logEvent) + config.separator; }
});
log4js.configure({
appenders: {
out: { type: 'stdout', layout: { type: 'json', separator: ',' } }
},
categories: {
default: { appenders: ['out'], level: 'info' }
}
});
const logger = log4js.getLogger('json-test');
logger.info('this is just a test');
We can use bunyan for same purpose which logs your console log in JSON format.
Here is the npm link for node : https://www.npmjs.com/package/bunyan.
Also even if you want to use log4js then create your own pattern and write log file into JSON format.
Thanks.

Google datastore.lookup in Node.js returns no results

I'm trying to run a simple lookup against my Google datastore data via the google-api-nodejs-client JS plugin. When I run this lookup using Google's "try it now" tool, it works fine:
Request
POST https://www.googleapis.com/datastore/v1beta2/datasets/healthier-staging/lookup?key={YOUR_API_KEY}
{
"keys": [
{
"path": [
{
"kind": "Subscriber",
"name": "+1215XXXXXXX"
}
]
}
]
}
Response
200 OK
{
"found": [
{
"entity": {
"key": {
"partitionId": {
"datasetId": "s~healthier-staging"
},
"path": [
{
"kind": "Subscriber",
"name": "+1215XXXXXXX"
}
]
},
"properties": {
"phone": {
...
However, when I run exactly the same query in Node.js, I get no results--no error, but also no results. I'm able to authenticate, connect to the datastore, create a transaction, etc. so it doesn't appear to be an authentication issue. Here's the Node code I'm running:
this.datastore.lookup({
datasetId : 'healthier-staging',
keys: [{ path: [{ kind: 'Subscriber', name: '+1215XXXXXXX' }] }]
},
(function(err, result) {
if (err) {
console.error(err);
return;
}
console.log(result);
}).bind(this));
And the console output is:
{ found: [], missing: [], deferred: [] }
Note: this is related to the issue I reported in Error every time I run datastore.runQuery: one of fields Query.query and Query.gql_query must be set where I am trying to read the same data using runQuery instead of lookup, but the issue I'm facing is different.
Thanks.
The keys should be part of the post payload:
this.datastore.lookup({
datasetId : 'healthier-staging',
resource: {
keys: [{ path: [{ kind: 'Subscriber', name: '+1215XXXXXXX' }] }]
}
},
(function(err, result) {
if (err) {
console.error(err);
return;
}
console.log(result);
}).bind(this));
You can find the source for the Datastore portion of the API here.

Extjs How to load a JSON folder structure into a tree?

I'm trying to create a Extjs tree with JSON data. The data I want to load into the tree contains a folder structure. But when I trie to load the data into the tree, it doesn't show anything.
I have checked the json code here (JSONLint) on errors but everythin looks fine. Which would say that the problem probably is in the extjs part.
I have no idea how to get it works.
I have created a JSON-object like this:
{
"folders": [
{
"name": "Function",
"id": "workspace://SpacesStore/000-000-000",
"folders": [
{
"name": "Evaluation reports",
"id": "workspace://SpacesStore/00-00-4949-9caf-6655fg"
},
{
"name": "Function Reports",
"id": "workspace://SpacesStore/554gg-563-sd555-872e-0098hhjf"
},
{
"name": "Training(POP)",
"id": "workspace://SpacesStore/4334g-67hj-4357-ba96-4343fhj343"
}
]
},
{
"name": "Application data",
"id": "workspace://SpacesStore/3434gg-a761-48a2-83fa-3434f454hu",
"folders": [
{
"name": "Application letters",
"id": "workspace://SpacesStore/23232ff-c95f-4999-sdsd556-00886ggh7765"
}
]
}
]
}
This is the Extjs part where I want to load the JSON data:
initComponent: function() {
// declare a new store and load tree data
this.store = new Ext.data.TreeStore({
// set params
proxy: {
type: 'ajax',
reader: 'json',
url: 'http://localhost:8080/testApp/rest/folder/1'
}
});
this.items = [{
flex: 1
}];
this.callParent();
}
You didn't tell the reader what property to read from:
reader: {
type: 'json',
rootProperty: 'folders'
}

Kendo grid: How to remote filter above 2 values from one column

I have one column in kendo grid but if i'm filtering opposite the server API i need to filter against two values.
It means something like this:
{
"entityName": "client",
"data": {
"take": 10,
"skip": 0,
"page": 1,
"pageSize": 10,
"filter": {
"logic": "and",
// IN FILTER IS IMPORTANT TO HAVE 2 OBJECTS
"filters": [
{
"operator": "eq",
"value": "test",
"field": "client.name"
},
{
"operator": "eq",
"value": "test",
"field": "client.surname"
}
]
},
"group": []
}
}
I tried it by this way:
filterable : {
cell :
[
{
dataTextField : "client.name",
operator : "contains"
},
{
dataTextField : "client.surname",
operator : "contains"
}
]
}
But without luck.
How can i do it please?
Many thanks for any advice.
In order to do this, you'll have to intercept the dataSource data request and change the filter inside the readOptions before it get to the server. You'll have to create a custom transport.read and each time the dataSource will request some data, it will pass the readOptions parameter to that function.
myGrid.kendoGrid({
dataSource: {
serverFiltering: true,
transport: {
read: function (readOptions) {
readOptions.filter.filters = [{
operator: "eq",
value: "test",
field: "client.name"
}, {
operator: "eq",
value: "test",
field: "client.surname"
}];
//Insert you logic to retrieve the data from the server
//You may also try to call the dataSource original read function and pass the modified readOptions
//The ajax is just an example...
$.ajax({data: readOptions}).done(function(data){
readOptions.success(data);
});
}
}
}
Remember that if you overwrite the read function, you are responsible to call the readOptions success / error function to notify the dataSource about the data callback.

Categories

Resources