gpt4 book ai didi

node.js - Sequelize 包含返回数据

转载 作者:太空宇宙 更新时间:2023-11-04 00:21:36 24 4
gpt4 key购买 nike

我使用 Sequelize Express 和 Node.js 作为后端。我有模型事件和模型买家与多对多的关系

模特事件:

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;
};

模特卖家:

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;
};

我运行查询并使用

发送数据
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;
});
});
};

在我收到的终端中:

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

如何将商品退回:

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

最佳答案

从查询中删除 raw: true这将为卖家提供每个事件的实例

类似

{
//Event1 properties
Sellers: {
// Associated sellers properties
}
}

我猜我参加聚会已经太晚了..但对于面临类似问题的其他人来说

关于node.js - Sequelize 包含返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44388335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com