System Information
- strapi v4:
- Linux Mint:
- Database Default:
- Node Version - 18:
- npm@9.6.7:
Hi everybody, im trying to create new api that gives all my datas from all collections ( api ) in one endpoint.
I wrote simple code after reading docs for while. but not succeeded can you please check what’s wrong in my code.
I wrote filenames as comment in code.
Thank you.
'use strict';
// services/everything.js
module.exports = {
fetchAllCollections: async () => {
try {
const collections = ["collection1", "collection2", "collection3", "collection4"];
let combinedData = [];
strapi.log.error('Combined Data');
for (const collection of collections) {
const entries = await strapi.query(collection).find();
combinedData.push(...entries);
}
return combinedData;
} catch (err) {
throw err;
}
},
};
'use strict';
// controllers/everything.js
module.exports = {
async fetchAllCollections(ctx) {
try {
const data = await strapi.service('everything').fetchAllCollections();
ctx.body = data;
} catch (err) {
ctx.badRequest("Fetch all collections error", { moreDetails: err });
}
},
};
'use strict';
// routes/everything.js
module.exports = {
routes: [
{
method: "GET",
path: "/fetch-all-collections",
handler: "everything.fetchAllCollections",
config: {
policies: [],
middlewares: [],
},
},
],
};