Я пытаюсь получить место по его идентификатору из базы данных, используя Sequelize в качестве ORM. Я настроил модели в ассоциации, принадлежащей Таким образом, 1 место может иметь 1 номер телефона и 1 адрес. Я прочитал документацию Sequelize, размещенную на их веб-сайте, но не нашел решения этой проблемы. Ниже я предоставил вам запрос, сообщение об ошибке и способ, которым модели связаны друг с другом.
Это запрос:
function findById(id) {
return this.findOne({
where: { id: id },
include: [
{
model: models.Address,
as: "venue_address"
},
{
model: models.PhoneNumber,
as: "phone_number"
}
]
}).then((venue) => {
if (!venue) {
return utils.createError(404, `Venue with id ${id} does not exist`);
}
return venue;
});
}
Sequelize возвращает следующую ошибку:
TypeError: Cannot read property 'getTableName' of undefined
at Function._validateIncludedElement (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:465:30)
at options.include.options.include.map.include (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:395:37)
at Array.map (<anonymous>)
at Function._validateIncludedElements (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:390:39)
at Promise.try.then.then (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/model.js:1576:14)
at tryCatcher (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:756:18)
at tryOnImmediate (timers.js:717:5)
at processImmediate [as _immediateCallback] (timers.js:697:5)
Unhandled rejection Error: Transaction cannot be rolled back because it has been finished with state: commit
at Transaction.rollback (/Users/kevin/Tabbs/API-Mysql/node_modules/sequelize/lib/transaction.js:85:35)
at models.Address.createAddress.then.catch (/Users/kevin/Tabbs/API-Mysql/server/routes/venue.js:57:13)
at tryCatcher (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/promise.js:689:18)
at Async._drainQueue (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/kevin/Tabbs/API-Mysql/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:756:18)
at tryOnImmediate (timers.js:717:5)
at processImmediate [as _immediateCallback] (timers.js:697:5)
Модели связаны так:
models.Venue.belongsTo(models.PhoneNumber, {
as: "phone_number",
onDelete: "CASCADE",
foreignKey: { allowNull: false }
});
models.Venue.belongsTo(models.Address, {
as: "venue_address",
onDelete: "CASCADE",
foreignKey: { allowNull: false }
});
Это то, что я получаю обратно, когда ничего не включаю:
Благодарим вас за помощь в устранении этой проблемы!