- Strapi Version: 4.10.1
- Operating System: ubuntu 22.04
- Database: SQLite
- Node Version: 18.14.0
- NPM Version: 9.3.1
I have created an api , using strapi , here is the code files :
routers:
module.exports = {
routes: [
{
method: 'GET',
path: '/energy-consumption',
handler: 'energy.energyConsumption',
config: {
policies: [],
middlewares: [],
},
},
{
method: 'POST',
path: '/energy-consumption',
handler: 'energy.energyConsumption',
config: {
policies: [],
middlewares: [],
},
},
],
};
controllers:
'use strict';
/**
* A set of functions called "actions" for `energy`
*/
module.exports = {
energyConsumption: async (ctx) => {
try {
console.log('Energy consumption action called');
const data = await strapi.service('api::energy.energy').find();
console.log('Retrieved energy consumption data:', data);
console.log("this is before the sending of the data");
ctx.send(data);
console.log("this is after the sending of the data ")
} catch (err) {
console.error('Error retrieving energy consumption:', err);
ctx.send({ error: err.message }, 500);
}
}
};
service file:
'use strict';
/**
* A set of functions called "actions" for `energy`
*/
module.exports = {
energyConsumption: async (ctx) => {
try {
console.log('Energy consumption action called');
const data = await strapi.service('api::energy.energy').find();
console.log('Retrieved energy consumption data:', data);
console.log("this is before the sending of the data");
ctx.send(data);
console.log("this is after the sending of the data ")
} catch (err) {
console.error('Error retrieving energy consumption:', err);
ctx.send({ error: err.message }, 500);
}
}
};
the problem is when i console log it , i can see the results of the energy data , here is the full log of the server :
Welcome back!
To manage your project 🚀, go to the administration panel at:
http://localhost:1337/admin
To access the server ⚡️, go to:
http://localhost:1337
Energy consumption action called
Connection to Elasticsearch successful
Sending search request to Elasticsearch
Retrieved energy consumption data: {
results: [],
pagination: { page: 1, pageSize: 25, pageCount: 0, total: 0 }
}
this is before the sending of the data
this is after the sending of the data
[2023-05-18 22:53:29.256] http: GET /api/energy-consumption (82 ms) 200
{
name: 'elasticsearch',
cluster_name: 'docker-cluster',
cluster_uuid: 'YhJENNwuRoCCtDdiz5-4kQ',
version: {
number: '8.6.2',
build_flavor: 'default',
build_type: 'docker',
build_hash: '2d58d0f136141f03239816a4e360a8d17b6d8f29',
build_date: '2023-02-13T09:35:20.314882762Z',
build_snapshot: false,
lucene_version: '9.4.2',
minimum_wire_compatibility_version: '7.17.0',
minimum_index_compatibility_version: '7.0.0'
},
tagline: 'You Know, for Search'
}
Elasticsearch response: {
took: 1,
timed_out: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: {
total: { value: 9, relation: 'eq' },
max_score: 1,
hits: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
}
}
Received response from Elasticsearch: {
took: 1,
timed_out: false,
_shards: { total: 1, successful: 1, skipped: 0, failed: 0 },
hits: {
total: { value: 9, relation: 'eq' },
max_score: 1,
hits: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
}
}
Total hits: 9
############### this is mappedData [
{
name: 'phase2',
measuringPoint: 'MY_SENSOR0',
current: 26.112,
voltage: 234.449,
activePower: 22517.918,
cosphi: 0.895,
powerFactory: 52.639,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase2',
measuringPoint: 'MY_SENSOR1',
current: 91.742,
voltage: 226.565,
activePower: 20896.89,
cosphi: 1.632,
powerFactory: 48.576,
date: '2023-05-10T15:48:00.566Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR2',
current: 115.652,
voltage: 239.214,
activePower: 27142.946,
cosphi: 0.737,
powerFactory: 49.227,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR0',
current: 45.439,
voltage: 243.47,
activePower: 17125.657,
cosphi: 0.6,
powerFactory: 51.718,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase2',
measuringPoint: 'MY_SENSOR2',
current: 96.779,
voltage: 232.078,
activePower: 18535.919,
cosphi: 1.585,
powerFactory: 48.039,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR2',
current: 75.899,
voltage: 222.088,
activePower: 910.571,
cosphi: 0.55,
powerFactory: 52.837,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR1',
current: 27.94,
voltage: 235.424,
activePower: 26032.483,
cosphi: 0.78,
powerFactory: 48.433,
date: '2023-05-10T15:48:00.566Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR0',
current: 116.696,
voltage: 225.73,
activePower: 26858.449,
cosphi: 1.103,
powerFactory: 49.435,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR1',
current: 102.288,
voltage: 223.414,
activePower: 24469.064,
cosphi: 0.239,
powerFactory: 48.159,
date: '2023-05-10T15:48:00.566Z'
}
]
{
name: 'phase2',
measuringPoint: 'MY_SENSOR0',
current: 26.112,
voltage: 234.449,
activePower: 22517.918,
cosphi: 0.895,
powerFactory: 52.639,
date: '2023-05-10T15:48:00.542Z'
}
{
name: 'phase2',
measuringPoint: 'MY_SENSOR1',
current: 91.742,
voltage: 226.565,
activePower: 20896.89,
cosphi: 1.632,
powerFactory: 48.576,
date: '2023-05-10T15:48:00.566Z'
}
{
name: 'phase3',
measuringPoint: 'MY_SENSOR2',
current: 115.652,
voltage: 239.214,
activePower: 27142.946,
cosphi: 0.737,
powerFactory: 49.227,
date: '2023-05-10T15:48:00.569Z'
}
{
name: 'phase1',
measuringPoint: 'MY_SENSOR0',
current: 45.439,
voltage: 243.47,
activePower: 17125.657,
cosphi: 0.6,
powerFactory: 51.718,
date: '2023-05-10T15:48:00.542Z'
}
{
name: 'phase2',
measuringPoint: 'MY_SENSOR2',
current: 96.779,
voltage: 232.078,
activePower: 18535.919,
cosphi: 1.585,
powerFactory: 48.039,
date: '2023-05-10T15:48:00.569Z'
}
{
name: 'phase1',
measuringPoint: 'MY_SENSOR2',
current: 75.899,
voltage: 222.088,
activePower: 910.571,
cosphi: 0.55,
powerFactory: 52.837,
date: '2023-05-10T15:48:00.569Z'
}
{
name: 'phase3',
measuringPoint: 'MY_SENSOR1',
current: 27.94,
voltage: 235.424,
activePower: 26032.483,
cosphi: 0.78,
powerFactory: 48.433,
date: '2023-05-10T15:48:00.566Z'
}
{
name: 'phase3',
measuringPoint: 'MY_SENSOR0',
current: 116.696,
voltage: 225.73,
activePower: 26858.449,
cosphi: 1.103,
powerFactory: 49.435,
date: '2023-05-10T15:48:00.542Z'
}
{
name: 'phase1',
measuringPoint: 'MY_SENSOR1',
current: 102.288,
voltage: 223.414,
activePower: 24469.064,
cosphi: 0.239,
powerFactory: 48.159,
date: '2023-05-10T15:48:00.566Z'
}
#############this is results [
{
name: 'phase2',
measuringPoint: 'MY_SENSOR0',
current: 26.112,
voltage: 234.449,
activePower: 22517.918,
cosphi: 0.895,
powerFactory: 52.639,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase2',
measuringPoint: 'MY_SENSOR1',
current: 91.742,
voltage: 226.565,
activePower: 20896.89,
cosphi: 1.632,
powerFactory: 48.576,
date: '2023-05-10T15:48:00.566Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR2',
current: 115.652,
voltage: 239.214,
activePower: 27142.946,
cosphi: 0.737,
powerFactory: 49.227,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR0',
current: 45.439,
voltage: 243.47,
activePower: 17125.657,
cosphi: 0.6,
powerFactory: 51.718,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase2',
measuringPoint: 'MY_SENSOR2',
current: 96.779,
voltage: 232.078,
activePower: 18535.919,
cosphi: 1.585,
powerFactory: 48.039,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR2',
current: 75.899,
voltage: 222.088,
activePower: 910.571,
cosphi: 0.55,
powerFactory: 52.837,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR1',
current: 27.94,
voltage: 235.424,
activePower: 26032.483,
cosphi: 0.78,
powerFactory: 48.433,
date: '2023-05-10T15:48:00.566Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR0',
current: 116.696,
voltage: 225.73,
activePower: 26858.449,
cosphi: 1.103,
powerFactory: 49.435,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR1',
current: 102.288,
voltage: 223.414,
activePower: 24469.064,
cosphi: 0.239,
powerFactory: 48.159,
date: '2023-05-10T15:48:00.566Z'
}
]
Transformed data:
{
name: 'phase2',
measuringPoint: 'MY_SENSOR0',
current: 26.112,
voltage: 234.449,
activePower: 22517.918,
cosphi: 0.895,
powerFactory: 52.639,
date: '2023-05-10T15:48:00.542Z'
}
{
name: 'phase2',
measuringPoint: 'MY_SENSOR1',
current: 91.742,
voltage: 226.565,
activePower: 20896.89,
cosphi: 1.632,
powerFactory: 48.576,
date: '2023-05-10T15:48:00.566Z'
}
{
name: 'phase3',
measuringPoint: 'MY_SENSOR2',
current: 115.652,
voltage: 239.214,
activePower: 27142.946,
cosphi: 0.737,
powerFactory: 49.227,
date: '2023-05-10T15:48:00.569Z'
}
{
name: 'phase1',
measuringPoint: 'MY_SENSOR0',
current: 45.439,
voltage: 243.47,
activePower: 17125.657,
cosphi: 0.6,
powerFactory: 51.718,
date: '2023-05-10T15:48:00.542Z'
}
{
name: 'phase2',
measuringPoint: 'MY_SENSOR2',
current: 96.779,
voltage: 232.078,
activePower: 18535.919,
cosphi: 1.585,
powerFactory: 48.039,
date: '2023-05-10T15:48:00.569Z'
}
{
name: 'phase1',
measuringPoint: 'MY_SENSOR2',
current: 75.899,
voltage: 222.088,
activePower: 910.571,
cosphi: 0.55,
powerFactory: 52.837,
date: '2023-05-10T15:48:00.569Z'
}
{
name: 'phase3',
measuringPoint: 'MY_SENSOR1',
current: 27.94,
voltage: 235.424,
activePower: 26032.483,
cosphi: 0.78,
powerFactory: 48.433,
date: '2023-05-10T15:48:00.566Z'
}
{
name: 'phase3',
measuringPoint: 'MY_SENSOR0',
current: 116.696,
voltage: 225.73,
activePower: 26858.449,
cosphi: 1.103,
powerFactory: 49.435,
date: '2023-05-10T15:48:00.542Z'
}
{
name: 'phase1',
measuringPoint: 'MY_SENSOR1',
current: 102.288,
voltage: 223.414,
activePower: 24469.064,
cosphi: 0.239,
powerFactory: 48.159,
date: '2023-05-10T15:48:00.566Z'
}
##############this is the results array [
{
name: 'phase2',
measuringPoint: 'MY_SENSOR0',
current: 26.112,
voltage: 234.449,
activePower: 22517.918,
cosphi: 0.895,
powerFactory: 52.639,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase2',
measuringPoint: 'MY_SENSOR1',
current: 91.742,
voltage: 226.565,
activePower: 20896.89,
cosphi: 1.632,
powerFactory: 48.576,
date: '2023-05-10T15:48:00.566Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR2',
current: 115.652,
voltage: 239.214,
activePower: 27142.946,
cosphi: 0.737,
powerFactory: 49.227,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR0',
current: 45.439,
voltage: 243.47,
activePower: 17125.657,
cosphi: 0.6,
powerFactory: 51.718,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase2',
measuringPoint: 'MY_SENSOR2',
current: 96.779,
voltage: 232.078,
activePower: 18535.919,
cosphi: 1.585,
powerFactory: 48.039,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR2',
current: 75.899,
voltage: 222.088,
activePower: 910.571,
cosphi: 0.55,
powerFactory: 52.837,
date: '2023-05-10T15:48:00.569Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR1',
current: 27.94,
voltage: 235.424,
activePower: 26032.483,
cosphi: 0.78,
powerFactory: 48.433,
date: '2023-05-10T15:48:00.566Z'
},
{
name: 'phase3',
measuringPoint: 'MY_SENSOR0',
current: 116.696,
voltage: 225.73,
activePower: 26858.449,
cosphi: 1.103,
powerFactory: 49.435,
date: '2023-05-10T15:48:00.542Z'
},
{
name: 'phase1',
measuringPoint: 'MY_SENSOR1',
current: 102.288,
voltage: 223.414,
activePower: 24469.064,
cosphi: 0.239,
powerFactory: 48.159,
date: '2023-05-10T15:48:00.566Z'
}
]
but when i try to get a get request for the api , or open the page on the browser , here is the error that i have :
{
"results": [],
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 0,
"total": 0
}
}
and i am not able to consume the data in the front end of my application .
i would like also to mention that i am querying elastic serearch , and as you can see , the query is correct , because i am able to see the data , in the console , but in same time , i am not able to use it ,for the front end
what am I doing wrong ? any suggestions ?