I have two models (Individual, Email) and am trying to insert into the created 'Individual_Email' table using the Sequelize commands. While Sequelize is creating the desired table, it returns the following error when trying to add/get/set to/from that table: "Object [object Promise] has no method 'addEmail'". What am I missing?
The Sequelize documentation says if the models are User and Project, "This will add methods getUsers, setUsers, addUsers to Project, and getProjects, setProjects and addProject to User."
This leads me to believe (looking at promises) that I'm likely misunderstanding how to use the asynchronous features of Node. I've tried both a synchronous and async version of insertion, both returning the same message above.
Sequelize Documentation: http://docs.sequelizejs.com/en/latest/docs/associations/
Code:
routes/index.js
var express = require('express');
var router = express.Router();
var models = require('../models');
/* GET home page. */
router.get('/', function(req, res, next) {
'use strict';
var tIndividual, tEmail;
tIndividual = models.Individual.create({
name: "Test"
});
tEmail = models.Email.create({
address: "test@gmail.com"
});
console.log(tEmail);
res.render('index', { title: 'Express' });
});
module.exports = router;
OR
/* GET home page. */
router.get('/', function(req, res, next) {
'use strict';
var tIndividual, tEmail;
tIndividual = models.Individual.create({
name: "Test"
}).then(function(){
tEmail = models.Email.create({
address: "test@gmail.com"
}).then(function(){
tIndividual.addEmail(tEmail).then(function(){
console.log("Success");
});
})
});
res.render('index', { title: 'Express' });
});
module.exports = router;
models/index.js
db.Individual.hasMany(db.Email, {
as: 'Email',
through: 'Individual_Email'
});
db.Email.hasMany(db.Individual, {
as: 'Individual',
through: 'Individual_Email'
});
How can I add to the table 'Individual_Email' in the best way? I'm assuming I need to do it synchronously to wait for the update, but I'm open to any suggestions. Thanks!