How do I return only selected certain Name fields in Strapi?

‘use strict’;
const { sanitizeEntity } = require(‘strapi-utils’);

/**

  • Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)

  • to customize this controller
    */
    module.exports = {
    // GET /hello
    async productSearch(ctx) {
    //const entity = await strapi.query(‘product’).model.fetchAll({ columns: [‘Name’,‘id’] })

    //const entity = await strapi.query(‘product’).model.fetchAll({ columns: [‘Name’, ‘id’] })

    const entity = await strapi.query(‘product’).model.fetchAll({ columns: [‘Name’,‘id’,‘product_sub_category’] })

    //return sanitizeEntity(entity, { model: strapi.models.test });
    const productObj = entity.map(entity => sanitizeEntity(entity, { model: strapi.models.product }));

    // let newObj = [];
    // productObj.forEach(products => {
    // newObj.push({Name:products.Name, LastName: products.LastName});
    // });
    return productObj;
    },
    };

Hey @aatishnimbokar :wave:,

Could you add a description on your problem and on what you are trying achieve.
Also try to use some formatting for code & text, so your post is more readable.

You can easily use the icons when creating or editing your post:
image

Maybe this post could help you on how to markup your post :wink::+1:

Thanks For Reply me
In Strapi API I am Able to show Name and id Field ,
Now I trying to show only specific name filed to json formate , I want to apply search for specific Name field.So Below code is given me only Id and Name filed & I want to apply search/filter by certain name .

For example if wanted to search “MattieBelt” at that time strapi show only MattieBelt information.

please help me or kindly provide me your contact if you unable to understand my problem.

-----------Below code I fetch id and name------
‘use strict’;
const { sanitizeEntity } = require(‘strapi-utils’);

module.exports = {
async productSearch(ctx) {

const entity = await strapi.query('product').model.fetchAll({ columns: ['Name','id'] })

const productObj = entity.map(entity => sanitizeEntity(entity, { model: strapi.models.product }));



return productObj;

},
};

Thanks & Regards
Aatish Nimbokar

Reply please!

Hey @aatishnimbokar,

Sorry for my late reaction, I’m doing this in my free time and had time for my family this weekend. :wink:

But after trying something, this should work for you.

strapi.query('product').model.query(qb => { 
    qb.where('Name', 'LIKE', 'MattieBelt'); 
}).fetchAll( {
    columns: ['Name','id']
});

Hope this helps.

Do note this way you are using the underlying database service bookshelf, checkout the docs:
https://strapi.io/documentation/developer-docs/latest/concepts/queries.html#custom-queries

No issue Sir!

Thanks a lot for reply me
I will apply this and let you know