Sails/Waterline 填充未按预期工作
Sails/Waterline Populate doesn't work as expected
最近几个小时我一直在用头撞墙,我想不出解决问题的办法。在我的风帆模型中,我有 2 个 one-to-many 关联。 'A'模型可以有很多'B',而'B'模型可以有很多'C'。在我的控制器中,当我执行 a.find().populate('b') (...)
时,它 returns 我是 A 模型的整个 collection,但是只用一个 B 填充 A 模型的每个条目,这与当前数据集不匹配我的数据库中有 (mysql)。并且不填充 B 模型中的 C 条目。换句话说,我正在尝试实现类似 嵌套人口的目标。
控制器代码有问题,对吧?我怎样才能使这项工作?
提前致谢!
编辑:
Company.js
module.exports = {
identity: 'company',
attributes: {
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
zip_code: {
type: 'string',
required: true
},
city: {
type: 'string',
required: true
},
nif: {
type: 'integer',
required: true,
minLength: 9
},
country: {
type: 'string',
required: true
},
phone_number: {
type: 'string',
required: true
},
email: {
type: 'email',
required: true
},
facilities: {
collection: 'facility',
references: 'facility',
on: 'id',
via: 'company'
}
}
};
Facility.js
module.exports = {
identity: 'facility',
attributes: {
company: {
columnName: 'id_company',
model: 'company'
},
warehouses: {
collection: 'warehouse',
references: 'warehouse',
on: 'id',
via: 'facility'
},
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
zip_code: {
type: 'string',
required: true
},
city: {
type: 'string',
required: true
},
country: {
type: 'string',
required: true
},
phone_number: {
type: 'string',
},
email: {
type: 'email',
},
longitude: {
type: 'float',
},
latitude: {
type: 'float'
}
}
};
Warehouse.js
module.exports = {
identity: 'warehouse',
attributes: {
facility: {
columnName: 'id_facility',
model: 'facility',
},
name: {
type: 'string',
required: true
},
longitude: {
type: 'float',
},
latitude: {
type: 'float'
}
}
};
MainController相关代码:
companies: function(req, res) {
company.find().populate('facilities').exec(function(err, comp){
var error = '';
if(err){
error = 'Unable to retrieve the requested information. Try again later and, if the problem persists, contact the platform administrator.';
} else if(!comp[0]) {
error = 'There\'s no company data inserted.';
}
// (...)
});
},
您应该从模型中删除 references
和 on
。
关于嵌套填充,正如我在评论中所说,Waterline目前不支持。您可以检查 Waterline2,正如他们所说,它提供了嵌套填充的可能性,但不建议用于生产。
否则你可以检查一下:Sails.js populate nested associations
最近几个小时我一直在用头撞墙,我想不出解决问题的办法。在我的风帆模型中,我有 2 个 one-to-many 关联。 'A'模型可以有很多'B',而'B'模型可以有很多'C'。在我的控制器中,当我执行 a.find().populate('b') (...)
时,它 returns 我是 A 模型的整个 collection,但是只用一个 B 填充 A 模型的每个条目,这与当前数据集不匹配我的数据库中有 (mysql)。并且不填充 B 模型中的 C 条目。换句话说,我正在尝试实现类似 嵌套人口的目标。
控制器代码有问题,对吧?我怎样才能使这项工作?
提前致谢!
编辑:
Company.js
module.exports = {
identity: 'company',
attributes: {
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
zip_code: {
type: 'string',
required: true
},
city: {
type: 'string',
required: true
},
nif: {
type: 'integer',
required: true,
minLength: 9
},
country: {
type: 'string',
required: true
},
phone_number: {
type: 'string',
required: true
},
email: {
type: 'email',
required: true
},
facilities: {
collection: 'facility',
references: 'facility',
on: 'id',
via: 'company'
}
}
};
Facility.js
module.exports = {
identity: 'facility',
attributes: {
company: {
columnName: 'id_company',
model: 'company'
},
warehouses: {
collection: 'warehouse',
references: 'warehouse',
on: 'id',
via: 'facility'
},
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
zip_code: {
type: 'string',
required: true
},
city: {
type: 'string',
required: true
},
country: {
type: 'string',
required: true
},
phone_number: {
type: 'string',
},
email: {
type: 'email',
},
longitude: {
type: 'float',
},
latitude: {
type: 'float'
}
}
};
Warehouse.js
module.exports = {
identity: 'warehouse',
attributes: {
facility: {
columnName: 'id_facility',
model: 'facility',
},
name: {
type: 'string',
required: true
},
longitude: {
type: 'float',
},
latitude: {
type: 'float'
}
}
};
MainController相关代码:
companies: function(req, res) {
company.find().populate('facilities').exec(function(err, comp){
var error = '';
if(err){
error = 'Unable to retrieve the requested information. Try again later and, if the problem persists, contact the platform administrator.';
} else if(!comp[0]) {
error = 'There\'s no company data inserted.';
}
// (...)
});
},
您应该从模型中删除 references
和 on
。
关于嵌套填充,正如我在评论中所说,Waterline目前不支持。您可以检查 Waterline2,正如他们所说,它提供了嵌套填充的可能性,但不建议用于生产。
否则你可以检查一下:Sails.js populate nested associations