Я хочу посчитать ассоциации городов с таблицей, которая называется маркером, но когда я вызываю код выше, у меня появляется ошибка выше. Ошибка не является достаточно явной. У меня есть опция freezeTableName в обеих таблицах
TypeError: attr [0] .indexOf не является функцией
модель города:
const models = require('../models2');
module.exports = (sequelize, DataTypes) => {
const City = sequelize.define('city', {
name: { type: DataTypes.STRING, allowNull: false },
status: { type: DataTypes.INTEGER, allowNull: false },
latitude: { type: DataTypes.BIGINT, allowNull: false },
longitude: { type: DataTypes.BIGINT, allowNull: false },
}, { freezeTableName: true});
City.associate = function(models) {
// associations can be defined here
City.hasMany(models.marker,{as: 'markers', foreignKey: 'cityId'})
};
return City;
};
* Маркер
модель:
module.exports = (sequelize, DataTypes) => {
const Marker = sequelize.define('marker', {
description: { type: DataTypes.STRING, allowNull: false },
status: { type: DataTypes.INTEGER, allowNull: false },
latitude: { type: DataTypes.BIGINT, allowNull: false },
longitude: { type: DataTypes.BIGINT, allowNull: false },
cityId: {
type: DataTypes.INTEGER,
references: {
model: 'city',
key: 'id',
}
}
}, { freezeTableName: true});
Marker.associate = function(models) {
// associations can be defined here
};
return Marker;
};
запрос на продолжение
City.findAll({
attributes: [
'name',
[sequelize.fn('count', sequelize.col('marker.cityId')) ,'marker_count']
],
include: [
{
model: Marker, as: "markers" ,
attributes: [] // <----- Make sure , this should be empty
}
]
}).then(cities => res.status(201).send(cities))
.catch(error => {
console.log(error);
res.status(400).send(error)
});