Return FetchResult element in js server function


#1

Hi, I’m trying to define a js server function that should return a FetchResult element.

In the SystemType I have the declaration:
getLevelsFacility: function(): FetchResult js server

In the js of the SystemType I implement the logic of getLevelsFacility(), at the end of the function I have (where array is a json):

var elementToRet = {
    "objs": array,
    "count":array.length,
    "hasMore":true
};
return elementToRet ;

Calling this function from a page in the UI I received the error
"message" : "Cannot instantiate type FetchResult, it is parametric and its variables aren't bound. Use the bound parametric type instead (usually reachable from a field)."

How can I do to return correctly a FetchResult object?


#2

I find a simil declaration, but it has the SystemType defined:
FetchResult<Measurement>

Maybe I should declare the SystemType to return…but if It doesn’t exists (I create a json “new”) how can I do? :slight_smile:


#3

FetchResult is a parametric type, you need to provide the type of objs in this fetch result, in your case it’s Facility, try the following syntax:

getLevelsFacility: function(): FetchResult<Facility> js server

The object in the javascript function is correct.


#4

The type does not exist, it is an aggregation of data taken from multiple types. Is there a way to pass to FetchResult a generic type?


#5

You need to provide a type otherwise it won’t work. If you want to return any kind of obj then try using Json.

getLevelsFacility: function(): FetchResult<Json> js server

#6

However, returning a Json is not recommended. If your function always returns the same kind of objects, you should create a type for that and return a FetchResult<ResultType>.
It will make your code much more clear and less error-prone.


#7

since the type does not exist you can create a not persistable type

type MyType {
field1
field2
filedN
}

Then into the .js for this new type you can do all the logic on the other types and return the myType objects - if the scope is to show this on UI then your data source should call MyType.myfunction


#8

Ok, I defined a non persitable type “SensorElementHierarchy” to return correctly FetchResult. Now I have another kind of problem.

I have a datasource element defined as

ui module Enterprise {
	dataSource MeasurementSeries {
		"c3type": "BasePhysicalMeasurementSeries",
		"c3function":"getLevelsFacility",
		"responseSelector": "objs"
	}
}

In the BasePhysicalMeasurementSeries, function getLevelsFacility, I return FetchResult<SensorElementHierarchy>

SensorElementHierarchy is defined as

type SensorElementHierarchy {
	servicePointName : string

	servicePointId : string

	measurementSeriesName : string	

	measurementSeriesId : string

	measurementType : string

	facilityNamePrincipal : string

	facilityIdPrincipal : string

	facilityNameLevel2 : string

	facilityIdLevel2 : string

	facilityNameLevel3 : string

	facilityIdLevel3 : string

	facilityNameLevel4 : string

	facilityIdLevel4 : string

	facilityNameLevel5 : string

	facilityIdLevel5 : string

	facilityNameLevel6 : string

	facilityIdLevel6 : string
	
}

Now in the UI page I have a component KendoGrid and I refer to this dataSource as

"data": {
"collection": "Enterprise.MeasurementSeries"
}

In the columns I can access to field of SensorElementHierarchy in “field” but not as link, for example

        "columns": [
            {
                "label": "{~Enterprise.SensorsListView.sensor~}",
                "field": "servicePointName",
                "format": "titleize",
                "width": 150,
                "align": "left",
                "sortable": true,
                "link": "/enterprise/servicePoint/{{servicePointId}}"
            }
        ],

I see correctly the name of ServicePoint (servicePointName) but the link doesn’t work , I have as link
"$root/enterprise/servicePoint/"

Any idea why it doesn’t work?


#9

I solved modifying the SensorElementHierarchy type adding the field “id” and in the function getLevelsFacility I return this type