'Need to handle the query string "/projects?technologies=AI&industries=Security" Using nodeJS
This is my first time to create a query string parameter, So please help me out.
- I want to filter the data based json field 'technologies, industries and maturity' every field(technologies, industries and maturity) have an array of elements. I want to filter that element of every field. enter image description here
- I wrote the backend code:
const { pick } = require('lodash');
const toArray = props =>
(props && props.split ? props.split(',') : props || []).map(item => item.trim());
console.log("this is in filter props: ", toArray)
module.exports = (entities, query) => {
const fields = toArray(query.pick);
const industries = toArray(query.industries);
const technologies = toArray(query.technologies);
const maturity = toArray(query.maturity);
return entities.reduce((accumulator, entity) => {
const hasTecnology = technologies.length
? technologies.every(technology => entity.technologies && entity.technologies.includes(technology))
: true;
const hasIndustury = industries.length
? industries.every(industry => entity.industries && entity.industries.includes(industry))
: true;
const hasMaturity = maturity.length
? maturity.every(maturty => entity.maturity && entity.maturity.includes(maturty))
: true;
const condition = hasTecnology && hasIndustury && hasMaturity;
return condition
? [...accumulator, fields.length ? pick(entity, fields) : entity]
: accumulator;
}, []);
};
- My controller function:
async find(ctx) {
const { query } = ctx.request;
const email = ctx.state.user.email;
const userProjects = strapi.cache.users[email].displayCatalog;
let projects = strapi.cache.projects.filter(project => userProjects.includes(project.slug));
if (query.pick || query.industries || query.technologies || query.maturity) {
projects = filter(projects, query);
}
return buildResponse(projects.map(project => hideSensitive(project, userProjects, email)));
},
- This code is valid for only single parameter "/projects?technologies=AI" but I am trying to get "/projects?technologies=AI&industries=Security&maturity=Experimentation" this type filter usign query string.
Note: If we select technologies field's element ex- Security or industries field's element ex- AI data will will show both technologies Security and industries AI.
Please help me out
Solution 1:[1]
you can query for all fields.
place this in controller.js file.
const queryObj = { ...req.query };
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(/\b(gt|gte|lt|lte|eq|ne)\b/g, match => `$${match}`);
console.log(queryStr);
const doc1 = await Model.find(JSON.parse(queryStr));
In the above code
api/projects?technologies[eq]="AI"&industries[eq]="Security"
Solution 2:[2]
- Thanks for Idea I changed some code and get the solution.
const toArray = props =>
(props && props.split ? props.split(',') : props || []).map(item => item.trim());
const hasField = (entity, fields) => {
if (!fields.length) {
return false;
}
for (let i = 0; i < fields.length; i + 1) {
if (entity && entity.includes(fields[i])) {
return true;
}
}
return false;
};
module.exports = (entities, query) => {
const industries = toArray(query.industries);
const technologies = toArray(query.technologies);
const maturity = toArray(query.maturity);
return entities.filter(entity => {
const hasTecnology = hasField(entity.industries, industries);
const hasIndustury = hasField(entity.technologies, technologies);
const hasMaturity = hasField(entity.maturity, maturity);
const condition = hasTecnology || hasIndustury || hasMaturity;
if (condition) {
return entity;
}
return null;
}, []);
};
- My controller will same
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | mohd kamran |