admin管理员组文章数量:1392007
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: '[email protected]'})
.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?
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: '[email protected]'})
.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?
Share Improve this question asked Apr 21, 2013 at 6:52 aishwaryaaishwarya 1,9861 gold badge15 silver badges22 bronze badges1 Answer
Reset to default 5the 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: '[email protected]'})
.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!
本文标签: javascriptSaving object with SequelizeStack Overflow
版权声明:本文标题:javascript - Saving object with Sequelize - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744766130a2624052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论