我无法在我的sails.js项目中使用populate() [英] I can't get populate() to work on my sails.js project

查看:227
本文介绍了我无法在我的sails.js项目中使用populate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使最简单的populate()查询在我的新sails.js项目上工作,我已经努力了几个小时.问题在于要填充的字段始终具有一个空数组.我已经仔细研究了这些示例,所以我认为我没有语法错误,但是也许我疲倦的眼睛没有让我看到.

I've been struggling for hours to get the simplest populate() query working on my new sails.js project. The problem is that the field to populate always has an empty array. I've pored over the examples, so I don't think I have a syntax error, but maybe my tired eyes aren't letting me see.

以下是进行查询的两种模型+方法:

Here are the two models + method that's making the query:

模特部:

    attributes: {
    id: {
        type: 'integer',
        primaryKey: true
    },
    name: {
        type: "string",
        required: true
    },
    shortName: {
        type: "string"
    },
    schoolName: {
        model: 'Schools'
    },
    courses: {
        collection: 'Courses',
        via: "departmentId"
    }
},

模型课程:

    attributes: {
    id: {
        type: "integer",
        primaryKey: true
    },
    name: {
        type: "string",
        required: true
    },
    shortName: {
        type: "string"
    },
    departmentId: {
        model: "Departments"
    }
},

最后,进行调用的方法:

And finally, the method making the call:

    getDepartmentsBySchool: function(schoolName, callback){
    Departments.find({schoolName: schoolName}).populate("courses").exec(function(error, departments){
        if(error){
            console.log("Departments could not be found");
            return callback(error, []);
        } else{
            console.log(departments.length + " departments found");
            return callback(null, departments);
        }
    });
}

结果:

courses: []

总是.

我正在使用MySQL,因此适配器为sails-mysql.任何类型的指针都将不胜感激,这样我就不必离开使用此框架了.

I am using MySQL, so the adapter is sails-mysql. Any sort of pointer would be greatly appreciated so that I don't have to move away from using this framework.

将迁移更改为更改"会导致以下结果:

Changing the migration to "alter" results in the following:

http://tinypic.com/r/9kv9ev/8

推荐答案

我在最后测试了您提供的代码示例.我没有保留shoolName作为对另一个集合的引用,而是将其保留为字符串,因为我想测试部门与课程之间的关联.一切似乎都正常.课程正在完美地填充.您可能想检查一件事.在使用以下类型的关联时:

I tested the code sample provided by you at my end. Instead of keeping shoolName as a reference to another collection, I had kept it as string since I wanted to test the association between departments and courses. Everything seems to work fine. Courses were getting populated perfectly. You might wanna check one thing. While using associations of the type:

   schoolName: {
        model: 'Schools'
    },

schoolName必须包含school表中的记录ID.希望您设置正确.

the schoolName must contain the id of record from schools table. I hope you are setting that up correctly.

有时这可能是与节点模块sails-mysql相关的问题.您可以尝试重新安装sails-mysql模块

Sometimes it can be an issue related to the node module sails-mysql. You may try reinstalling sails-mysql module

npm uninstall sails-mysql
npm cache clear
npm install sails-mysql

让我知道这是否可行.

Let me know if this works or not.

这篇关于我无法在我的sails.js项目中使用populate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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