Tuesday, 6 June 2017

Sequelize include return data

I am using Sequelize express with Node.js as the backend. I have models Event and models buyer with relation many to many

Model Event:

module.exports = function (sequelize, DataTypes) {
    var Event = sequelize.define("Event", { //...}, 
{
            tableName: 'event',
            timestamps: false,
            freezeTableName: true,
            classMethods: {
                associate: function (models) {
                    Event.hasMany(models.Meetings, {foreignKey: 'event_id'});

                Event.belongsToMany(models.Buyer, {
                    through: 'BuyerPresent'});

                Event.belongsToMany(models.Seller, {
                    through: 'SellerPresent'});
                }
            }
        });

    return Event;
};

Model Seller:

module.exports = function (sequelize, DataTypes) {
    var Seller = sequelize.define("Seller", { //...
    }, {
        tableName: 'seller',
        timestamps: false,
        freezeTableName: true,
        classMethods: {
            associate: function (models) {
                Seller.hasMany(models.Meetings, {foreignKey: 'seller_id'});

                Seller.belongsToMany(models.Event, {
                    through: 'SellerPresent'});
            }
        }
    });

    return Seller;
};

I run the query and send the data with

res.render('path', {'results':seller})

:

function findIdSeller(selectEvent) {
    var match = {
        where: {
            event: selectEvent
        },
        // include: {
        //     model: Seller
        // },
        include: [{
            model: Seller,
            nested: false
        }],
        // plain:true

        raw: true
    };
    return models.Event.findAll(match)
}
exports.findDataSellerPromise = function (selectEvent) {
    return findIdSeller(selectEvent).then(function (result) {
        return result.map(function(seller) {
            console.log('SELLINFO: ', seller);
            return seller;
        });
    });
};

In the terminal i received:

{
    id: 4,
    incoming: 2,
    event: 4,
    'Sellers.id': 1,
    'Sellers.name_firm': 'kdm srl',
    'Sellers.site': 'www.terrebasse.com'
}

How can I return an item as:

{
    id: 4,
    incoming: 2,
    event: 4,
    Sellers: {
        id': 1,
        name_firm: 'kdm srl',
        site: 'www.terrebasse.com'
    }
}



via Gig90

No comments:

Post a Comment