使用Sequelize保存对象

时间:2013-04-21 06:52:59

标签: javascript sequelize.js

我正在与Sequelize的第一步挣扎。我已经阅读了教程,我必须遗漏一些基本的东西,因为我已经花了一天时间试图弄清楚我做错了什么。

以下是我用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);
        });

    })
  })
})

它运行正常,我现在已经删除了断言。问题是,我没有看到任何一个控制台日志,没有成功而不是错误。另外,数据库中没有行。

db.js只是一个实用程序文件,它有助于使用db配置创建Sequelize JS的实例 - 我已经完成了一个console.dir并查看了实例,它看起来如下所示:

{ 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] } } }

现在我在屏幕上记录了INSERT语句,但实例从未进入数据库。我做错了什么?

1 个答案:

答案 0 :(得分:5)

问题不在于sequelize,而是因为它是异步代码。要解决此问题,只需修改测试:

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();
        });

    })
  })
})

添加回调(完成)有帮助!