首页 > 解决方案 > 如何在sails js中使用填充子标准(例如:.populate('something',{select:[]}))

问题描述

.populate('something',{select:[]})) in sails js .对我来说,这是一个模棱两可的用法。是否有任何替代解决方案

我在填充中使用了 select ...但它显示子标准在这个版本的风帆中不起作用

Banktransaction.find(newData).populate('project_id',{select:['project_name']}).exec((err,banktrans)=>{
                                    if(err){
                                        return res.json({
                                            error : err
                                            });
                                        }   
                                        if(!banktrans){
                                            return res.notFound();
                                        }else{
                                            return res.json({
                                                'responseType':"success",
                                                'responseMessage':"Banktransaction details founded successfully",
                                                'result': banktrans
                                            });
                                        }



                                });

//result

{
    "error": {
        "name": "UsageError",
        "code": "E_INVALID_POPULATES",
        "details": "Could not populate `project_id` because of ambiguous usage.  This is a singular (\"model\") association, which means it never refers to more than _one_ associated record.  So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association, since it generally wouldn't make any sense.  But that's the trouble-- it looks like some sort of a subcriteria (or something) _was_ provided!\n(Note that subcriterias consisting ONLY of `omit` or `select` are a special case that _does_ make sense.  This usage will be supported in a future version of Waterline.)\n\nHere's what was passed in:\n{ select: [ 'project_name' ] }"
    }
}

标签: sails.js

解决方案


您现在可能已经想通了,但是.populate()有限,有时风帆的文档是错误的。我专门编写了一个帮助程序来填充基于 3rd-normal O2M/M2M 关联表。我的方法还需要模型方法,给出每个关联字段名称的基本配置和对其模型类的引用。这并不优雅,它是对每条记录的单独查询,但也许我的方法的一些细节可能会让您感兴趣:

我的填充助手

示例用法。


推荐阅读