Aus irgendeinem Grund findet er nicht die Assoziation. Und ich habe die Assoziation geschaffen und der Fremdschlüssel schafft perfekt in Postgres."Benutzer ist nicht mit Feed verbunden!" sequelizejs
user.js
'use strict';
module.exports = app => {
const sequelize = app.db_connect.postgres.connect;
const Sequelize = app.db_connect.postgres.require;
const Reputation = app.models.reputation;
const Report = app.models.report;
const Group = app.models.group;
const Participant = app.models.participant;
const Topic = app.models.topic;
const Feed = app.models.feed;
const User = sequelize.define('user', {
//atributes
});
User.belongsToMany(User, { as: 'User', through: 'relationship', foreignKey: 'userId' });
User.belongsToMany(User, { as: 'Following', through: 'relationship', foreignKey: 'followingId' });
User.belongsToMany(Group, { as: 'Member', through: Participant, foreignKey: 'userId' });
User.hasMany(Report);
User.hasMany(Topic);
User.hasMany(Feed); //associate
return User;
}
feed.js
'use strict';
module.exports = app => {
const sequelize = app.db_connect.postgres.connect;
const Sequelize = app.db_connect.postgres.require;
const Report = app.models.report;
const Feed = sequelize.define('feed', {
//atributes
});
Feed.hasMany(Feed, {as: 'father'});
Feed.hasMany(Report)
return Feed;
}
Anruf:
const User = app.models.user;
const Sequelize = app.db_connect.postgres.require;
Feed.findOne({
include: User,
where: {
id: req.params.id
}
})
.then(result => res.json(result))
.catch(error => {
res.status(412).json({msg: error.message});
});
Benutzer und Futtermittel hat ein assoziiertes von 1: n. Also habe ich die hasMany Sequelize verwendet.