[英]How do I access records in node.js using 'has-many belongs-to relationaship'?
I am new to node.js. 我是node.js的新手。 I am using sequelize in my app.
我在我的应用程序中使用sequelize。 I have the following models:
我有以下型号:
var Topic = sequelize.define('Topic', {
topic_name: Sequelize.STRING,
topic_category: Sequelize.STRING
},{tableName: 'Topics'})
var RawStatistic = sequelize.define('RawStatistic', {
feedback_value: Sequelize.STRING,
count: Sequelize.INTEGER,
total_feedbacks_for_this_topic: Sequelize.INTEGER
},{tableName: 'RawStatistics'})
And the rellation between them as follows: 它们之间的关系如下:
RawStatistic.belongsTo(Topic);
Topic.hasMany(RawStatistic);
But when I use: 但是当我使用时:
RawStatistic.all({limit: FIVE}).success(function (raw_statistics) {
raw_statistics.getTopic().success(function (r){
res.json(r)
});
});
I get the error: 我收到错误:
TypeError: Object [object Object] has no method 'getTopic'
My question is how do I access a particular Topic name if I have a Feedback record? 我的问题是,如果我有反馈记录,如何访问特定的主题名称? What is the syntax for the same?
它的语法是什么? Probably I am overlooking an obvious syntax to achieve this.
可能我忽略了一个明显的语法来实现这一点。 Any help would be much appreciated.
任何帮助将非常感激。 Thanks in advance:)
提前致谢:)
You're trying to use an instance method on a collection of instances. 您正尝试在实例集合上使用实例方法。 You need to iterate over the result:
你需要迭代结果:
RawStatistic.all({limit: FIVE}).success(function (raw_statistics) {
var i;
for(i = 0; i < raw_statistics.length; ++i){
raw_statistics[i].getTopic().success(function (t){
/* ... */
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.