gpt4 book ai didi

mysql - 我无法让 populate() 处理我的 sails.js 项目

转载 作者:行者123 更新时间:2023-11-30 22:53:48 25 4
gpt4 key购买 nike

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

这是进行查询的两个模型和方法:

模型部门:

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

示范类(class):

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

最后,进行调用的方法:

    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。任何类型的指针都将不胜感激,这样我就不必放弃使用这个框架。

编辑:

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

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

最佳答案

我最后测试了您提供的代码示例。我没有将 shoolName 保留为对另一个集合的引用,而是将其保留为字符串,因为我想测试部门和类(class)之间的关联。一切似乎都很好。类(class)正在变得完美。你可能想检查一件事。使用以下类型的关联时:

   schoolName: {
model: 'Schools'
},

学校名称必须包含学校表中记录的 ID。我希望你设置正确。

有时它可能是与 Node 模块 sails-mysql 相关的问题。您可以尝试重新安装 sails-mysql 模块

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

让我知道这是否有效。

关于mysql - 我无法让 populate() 处理我的 sails.js 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27264828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com