How I can display category name?
I have two tables, prod
and category
and now i want to make a join between this table to display for fetch prod your's category. When i add a now product, i specific category id.
Here another information
Prods.belongsTo(Categs, {foreignKey: 'categ_id', targetKey: 'id'}) ,
Prods
and Categs
are two model make with sequelize.
Prods
att: id
, name
, category_id
;
Categs
att: id
, name
;
In prods.js i have :
function save() {
var form = $('#re_form').serializeObject();
if(form.id) {
update(form);
} else {
create(form);
}
}
function create(form) {
$.ajax({
url: '/prods/',
type: 'POST',
accepts: {
json: 'application/json'
},
data: form,
success: function(data) {
var row = '<tr id="row_id_'+ data.id +'">'
+ getColumns(data)
+ '</tr>';
$('#ar').append(row);
}
});
}
function getColumns(value) {
return '<td>'+value.id+'</td>'
+ '<td class="name">'+value.categ.name+'</td>'
+ '<td class="name">'+value.name+'</td>'
}
In HTML :
<div>
<label for="name">id Categ</label>
<input type="text" name="categ_id" id="categ_id" class="form-control"/>
</div>
Error: Uncaught Type Error: Cannot read property 'name' of undefined for category name