Saving object with Sequelize
javascript, sequelize.js
Solution
the problem was not with sequelize, but with the fact that it was async code. To fix this, simply modify the test:
var db = require("../../db.js").sequelize;
var DataTypes = require("sequelize");
var _table = db.define('users', {
name: DataTypes.STRING,
email: DataTypes.STRING
}, {
timestamps: false
});
var assert = require("assert")
describe('Users', function(){
describe('#save()', function(){
it('should save a user', function(done){
_table
.build({name: 'test', email: 'a@a.com'})
.save()
.success(function(o){
console.log("saved");
console.log(o.values);
done();
}).error(function(error) {
console.log("++++++++++");
console.log(error);
done();
});
})
})
})
Adding the callback (done) helps!
Problem
I am struggling with step 1 of Sequelize. I have read through the tutorial and I must be missing on something basic, for I have already spent over a day trying to figure out what I am doing wrong. The following is a unit test I have written using Mocha. ``` var db = require("../../db.js").sequelize; var DataTypes = require("sequelize"); var _table = db.define('users', { name: DataTypes.STRING, email: DataTypes.STRING }, { timestamps: false }); var assert = require("assert") describe('Users', function(){ describe('#save()', function(){ it('should save a user', function(){ _table .build({name: 'test', email: 'a@a.com'}) .save() .success(function(o){ console.log("saved"); console.log(o.values); }).error(function(error) { console.log("++++++++++"); console.log(error); }); }) }) }) ``` It runs fine and I have removed the asserts for now. The problem is, I don't see either of the console logs, not success not error. Plus there's no row in the database. The db.js is just a utility file that helps create an instance of Sequelize JS with the db configuration - I have already done a console.dir and looked through the instance and it looks as below: ``` { options: { dialect: 'mysql', host: 'localhost', port: 3306, protocol: 'tcp', define: {}, query: {}, sync: {}, logging: [Function], omitNull: false, queue: true, native: false, replication: false, pool: { maxConnections: 10, minConnections: 0, maxIdleTime: 1000 } }, config: { database: 'beacon', username: 'beacon', password: 'beacon', host: 'localhost', port: 3306, pool: { maxConnections: 10, minConnections: 0, maxIdleTime: 1000 }, protocol: 'tcp', queue: true, native: false, replication: false, maxConcurrentQueries: undefined }, daoFactoryManager: { daos: [ [Object], [Object] ], sequelize: [Circular] }, connectorManager: { sequelize: [Circular], client: null, config: { database: 'beacon', username: 'beacon', password: 'beacon', host: 'localhost', port: 3306, pool: [Object], protocol: 'tcp', queue: true, native: false, replication: false, maxConcurrentQueries: undefined }, disconnectTimeoutId: null, queue: [], activeQueue: [ [Object] ], maxConcurrentQueries: 50, poolCfg: { maxConnections: 10, minConnections: 0, maxIdleTime: 1000 }, pendingQueries: 0, useReplicaton: false, useQueue: true, pool: { destroy: [Function], acquire: [Function], borrow: [Function], release: [Function], returnToPool: [Function], drain: [Function], destroyAllNow: [Function], getPoolSize: [Function], getName: [Function], availableObjectsCount: [Function], waitingClientsCount: [Function] }, isConnecting: false }, importCache: {}, queryInterface: { sequelize: [Circular], QueryGenerator: { createTableQuery: [Function], dropTableQuery: [Function], renameTableQuery: [Function], showTablesQuery: [Function], addColumnQuery: [Function], removeColumnQuery: [Function], changeColumnQuery: [Function], renameColumnQuery: [Function], selectQuery: [Function], insertQuery: [Function], updateQuery: [Function], deleteQuery: [Function], incrementQuery: [Function], addIndexQuery: [Function], showIndexQuery: [Function], removeIndexQuery: [Function], getWhereConditions: [Function], hashToWhereConditions: [Function], attributesToSQL: [Function], findAutoIncrementField: [Function], addQuotes: [Function], removeQuotes: [Function], options: [Object] } } } ``` Now I get the INSERT statement logged on the screen, but the instance never makes it to the db. what am I doing wrong?