从MySQL表获取数据

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

You need to join the tables. Since both tables have id and name columns, you need to use an alias to distinguish them.

SELECT p.id AS product_id, p.name AS product_name, c.id AS category_id, c.name AS category_name
FROM prods AS p
JOIN Categs AS c ON p.category_id = c.id

Then you can use value.product_name and value.category_name to access these results in JavaScript.