get Selected fields in .populate() waterline-postgresql .populate('fieldName',{select:[]})

空扰寡人 提交于 2019-12-07 12:44:46

问题


select query is not working in .populate() of waterline-postgresql.

Model.find(query).populate(assoc.alias,{select:['field1','field2']});

This is not working in waterline-postgresql adapter.

Is this not supported or Am I making any mistake?


回答1:


select is not supported in .populate(). You can see this github issue. In populate select is not working currently.

This is feature request and it is open issue. hope in next release waterline team will introduce this feature.




回答2:


Since there's no official way to do that I did some changes in order to have that. Maybe it's not the best way but it works.

User.find({ belongs_to: user.id })
.populate('person_id')
.exec(function findCB (err, usersFound)
{
    console.log(usersFound[0].toJSON())

    if (err)
    {
        return res.json(401, {error: err});
    }

    if (usersFound.length == 0)
    {
        return res.json(200, {message: 'No user asigned'});
    }
    else
    {
        // An array to store all the final user objects
        var asignedArray = [];

        for (index in usersFound)
        {
            var myObj = {},
                key,
                value;

            // Object in format {key: value}
            myObj['id'] = usersFound[index].id;
            myObj['fullname'] = usersFound[index].person_id.first_name + ' ' +
                                usersFound[index].person_id.second_name + ' ' +
                                usersFound[index].person_id.last_name;
            myObj['email'] = usersFound[index].email;
            myObj['job'] = usersFound[index].person_id.job;
            myObj['permission_level'] = usersFound[index].permission_level;

            // Adding the object to the main array
            asignedArray.push(myObj);
        }

        return res.json(200, {
            users: asignedArray
        });
    }
    });

I hope it could be useful for you.



来源:https://stackoverflow.com/questions/31292018/get-selected-fields-in-populate-waterline-postgresql-populatefieldname-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!