bookshelf icon indicating copy to clipboard operation
bookshelf copied to clipboard

if search then return empty object instead of omit

Open durai001 opened this issue 5 years ago • 1 comments

Problem

if search child ( with related table) if there is no value then the whole JSON result should omit but I'm getting the empty object. thanks in advance

Issue Description

let say if I have a process table and user table. I am fetching the processing table with belongs to user table. below is the sample response

{
 {
        "id": 1,
        "process_id": 1,
        "description": "activity1-1-1",
        "user": {
            "id":1,
            "name": "durai"    
        }
    },
 {
        "id": 1,
        "process_id": 1,
        "description": "activity1-1-1",
         "user": {
            "id":2,
            "name": "vinoth"    
        }
    },
}

Expected behavior

the first process object is removed because I search with vinoth.The whole process object is needed to be removed

{
 {
        "id": 1,
        "process_id": 1,
        "description": "activity1-1-1",
         "user": {
            "id":2,
            "name": "vinoth"    
        }
    },
}

Actual behavior

If I search username "vinoth" then it's giving the empty object of other objects but I need that should be omitted.

{
 {
        "id": 1,
        "process_id": 1,
        "description": "activity1-1-1",
        "user": {},
    },
 {
        "id": 1,
        "process_id": 1,
        "description": "activity1-1-1",
         "user": {
            "id":2,
            "name": "vinoth"    
        }
    },
}

Here is the code

get_process: function (req, res) {
   process.where({
     id: req.params.id
   }).fetchAll({
     withRelated: [{
       "user": query => {
             if (search) {
               return query.where('name', 'LIKE', '%' + search + '%')
             }},
     }],
   }).then(function (process) {
     successHandler(200, process, res);
   })["catch"](function (err) {
     failureHandler(204, err, res);
   });
 },

durai001 avatar Nov 03 '20 08:11 durai001

My problem is if there is no data in relation then we should omit that whole process object not just like user kay . we no need that process details if there is no user with I searched

durai001 avatar Nov 11 '20 10:11 durai001