A simple snippet sample would be appreciated.
4 Answers, 1 is accepted
In order to return a data with relation, in the cloud code you will need to make a request to the api, using an expand expression, like this:
Everlive.CloudFunction.onRequest(function(request, response, done){ var expandExpression = { Role: true }; Everlive.Sdk.withMasterKey().data('Users').expand(expandExpression).get() .then(function(data){ response.body = data; done(); }, function(err){ response.statusCode = 400; response.body = err; done(); });});Using power fields is similar, with the difference you will need to use the 'X-Everlive-Power-Fields' header, like this:
Everlive.CloudFunction.onRequest(function(request, response, done){ var powerFieldsExpression = { UsersCount: { "contentType" : "Users", "queryType" : "count" } }; Everlive.Sdk.withMasterKey().data('Users') .withHeaders({'X-Everlive-Power-Fields': JSON.stringify(powerFieldsExpression)}) .get() .then(function(data){ response.body = data; done(); }, function(err){ response.statusCode = 400; response.body = err; done(); });});For more information about defining relation and power fields expressions, you can check the following documentation articles:
- Defining power fields
- Specifying an expand definition
I hope this helps.
Regards,
Anton Sotirov
Telerik
How would I modify this to include powerfields?
var dataProvider = app.everlive,
dataSourceOptions = {
type: 'everlive',
transport: {
typeName: 'Quest',
dataProvider: dataProvider
},
schema: {
model: {
fields: {
'Name': {
field: 'Name',
defaultValue: ''
},
'Description': {
field: 'Description',
defaultValue: ''
},
'QuestTypeID': {
field: 'QuestTypeID',
defaultValue: ''
},
'TeamTypeID': {
field: 'TeamTypeID',
defaultValue: ''
},
'RatingCount': {
field: 'RatingCount',
defaultValue: ''
},
'RatingAverage': {
field: 'RatingAverage',
defaultValue: ''
},
}
}
},
serverSorting: true,
serverPaging: true,
pageSize: 5
}var dataProvider = app.everlive,
dataSourceOptions = {
type: 'everlive',
transport: {
typeName: 'Quest',
dataProvider: dataProvider
},
schema: {
model: {
fields: {
'Name': {
field: 'Name',
defaultValue: ''
},
'Description': {
field: 'Description',
defaultValue: ''
},
'QuestTypeID': {
field: 'QuestTypeID',
defaultValue: ''
},
field: 'RatingAverage',
defaultValue: ''
},
}
}
},
serverSorting: true,
serverPaging: true,
pageSize: 5
}
So that, QuestTypeID and TeamTypeID returned the associated data?
​
The better approach would be to use expand expression in order to get the additional data.
The expand expression definition is explained here - link.
For example:
{ "QuestTypeID": { "TargetTypeName" : "<QuestTypeName-Here",
"ReturnAs": "QuestTypeIdExpanded",
"SingleField": "<the-field-you-want>"
}, TeamTypeID"": { "TargetTypeName" : "TeamsTypeName-Here",
"ReturnAs": "TeamTypeIdExpanded",
"SingleField": "<the-field-you-want>"
}}Then set the expand header in the datasource as shown in the example here - link.
read: { headers: { "X-Everlive-Expand": JSON.stringify(expandExpression) }}If you need more help, do not hesitate to write us back.
Regards,
Dimitar Dimitrov
Telerik