从sails控制器中的方法中排除某些数据的最佳方法 [英] the best way to exclude some data from a method in sails controller

查看:34
本文介绍了从sails控制器中的方法中排除某些数据的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些控制器方法中排除一些数据,而在其他方法中我想要的是这些数据.找到后,我使用 forEach 函数直接进入方法:

I want's to exclude some data in some controller method and in other method i want's that data. I do it with forEach function right into method after finding that :

nine: function (req, res) {
    Dore.find()
        .limit(9)
        .sort('createdAt DESC')
        .populate('file')
        .exec(function (err, sh) {
            if (err) {
                return res.negotiate(err);
            } else {
                console.log('before : ', sh);
                sh.forEach(function (item, i) {
                    delete item.zaman;
                });
                console.log('after : ', sh);
                return res.send(sh);
            }
        });
},

我想知道如何通过查找来做到这一点,并且永远不会包含在查找中,因此我们不需要使用 forEach 再次删除它.坦克

I want to know how possible to do that with finding and do not included ever in finding so we don't need to remove that again with forEach. tanks

正如@zabware 所说,我们在 Query option 中有 select 方法,我尝试这种格式但不起作用并返回所有数据:

As @zabware say we have select method in Query option I try this format but do not work and return all data :

我尝试使用以下格式但不起作用:

I try to use that with following format but don't working :

Model.find( {
            where: {},
            limit: 9,
            sort: 'createdAt DESC'
        },
        {
            select: [ 'id', 'createdAt' ]
        } )

Model.find( {
            where: {},
            limit: 9,
            sort: 'createdAt DESC',
                        select: [ 'id', 'createdAt' ]
        } )

Model.find( {}, select: [ 'id', 'createdAt' ] )

推荐答案

虽然 toJson 确实是为解决您面临的问题而设计的,它不会帮助您,因为在某些操作中您想排除某些字段,而在其他操作中则不想.

Although toJson is really designed to solve the kind of problem you are facing, it will not help you, since in some actions you want to exclude certain fields and in others not.

所以我们需要为每个查询选择字段.幸运的是,水线中有一个解决方案:

So we need to select fields per query. Luckily there is a solution for this in waterline:

Dore.find({}, {select: ['foo', 'bar']})
    .limit(9)
    .sort('createdAt DESC')
    .populate('file')
    .exec(function (err, sh) {
        console.log(err);
        console.log(sh);
    });

这是从sails 0.11 开始支持的,但没有真正记录下来.您可以在此处的查询选项下找到它 https://github.com/balderdashy/waterline-docs/blob/e7b9ecf2510646b7de69663f709175a186da10d5/queries/query-language.md#query-options

This is supported since sails 0.11 but not really documented. You can find it under query options here https://github.com/balderdashy/waterline-docs/blob/e7b9ecf2510646b7de69663f709175a186da10d5/queries/query-language.md#query-options

这篇关于从sails控制器中的方法中排除某些数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆