开发者问题收集

sequelize-cli db:seed 返回“未处理的拒绝 TypeError:未定义不是一个函数”

2015-10-01
1887

我正在使用 sequelize-cli (v1.9.1),并在运行 db:migration 后尝试播种,但在运行 db:seed 时我不断收到以下错误消息:

Sequelize [Node: 0.12.7, CLI: 1.9.1, ORM: 3.9.0]

Loaded configuration file "src/server/config/config.json".
Using environment "development".
Using gulpfile /usr/local/lib/node_modules/sequelize-cli/lib/gulpfile.js
Starting 'db:seed'...
Finished 'db:seed' after 96 ms
== 20151001101051-admin-user-seed: migrating =======
Unhandled rejection TypeError: undefined is not a function
    at Object.SqlString.escape (/Users/me/WebstormProjects/my-project/node_modules/sequelize/lib/sql-string.js:87:15)
    at Object.QueryGenerator.escape (/Users/me/WebstormProjects/my-project/node_modules/sequelize/lib/dialects/postgres/query-generator.js:924:22)
    at Object.<anonymous> (/Users/me/WebstormProjects/my-project/node_modules/sequelize/lib/dialects/postgres/query-generator.js:356:23)
    at Array.map (native)
    at Object.<anonymous> (/Users/me/WebstormProjects/my-project/node_modules/sequelize/lib/dialects/postgres/query-generator.js:352:23)
    at arrayEach (/Users/me/WebstormProjects/my-project/node_modules/lodash/index.js:1289:13)
    at Function.<anonymous> (/Users/me/WebstormProjects/my-project/node_modules/lodash/index.js:3345:13)
    at Object.QueryGenerator.bulkInsertQuery (/Users/me/WebstormProjects/my-project/node_modules/sequelize/lib/dialects/postgres/query-generator.js:350:13)
    at QueryInterface.bulkInsert (/Users/me/WebstormProjects/my-project/node_modules/sequelize/lib/query-interface.js:576:33)
    at Object.module.exports.up (/Users/me/WebstormProjects/my-project/src/server/seeders/20151001101051-admin-user-seed.js:17:31)
    at Object.tryCatcher (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/util.js:26:23)
    at Object.ret (eval at <anonymous> (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/promisify.js:163:12), <anonymous>:13:39)
    at module.exports.redefine.Class._exec (/usr/local/lib/node_modules/sequelize-cli/node_modules/umzug/lib/migration.js:49:23)
    at module.exports.redefine.Class.up (/usr/local/lib/node_modules/sequelize-cli/node_modules/umzug/lib/migration.js:33:17)
    at null.<anonymous> (/usr/local/lib/node_modules/sequelize-cli/node_modules/umzug/index.js:78:28)
    at Object.tapHandler (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/finally.js:63:44)
    at Object.tryCatcher (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/promise.js:507:31)
    at Promise._settlePromiseAt (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/promise.js:581:18)
    at Promise._settlePromises (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/promise.js:697:14)
    at Async._drainQueue (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/usr/local/lib/node_modules/sequelize-cli/node_modules/bluebird/js/main/async.js:133:10)

这是我的种子文件的样子:

'use strict';

module.exports = {
    up: function (queryInterface, Sequelize) {

        /*
         Add altering commands here.
         Return a promise to correctly handle asynchronicity.

         Example:
         return queryInterface.bulkInsert('Person', [{
         name: 'John Doe',
         isBetaMember: false
         }], {});
         */
        return queryInterface.bulkInsert('Users', [{
            id: Sequelize.UUIDV4,
            username: '[email protected]',
            email: '[email protected]'
        }], {});
    },

    down: function (queryInterface, Sequelize) {
        /*
         Add reverting commands here.
         Return a promise to correctly handle asynchronicity.

         Example:
         return queryInterface.bulkDelete('Person', null, {});
         */
        return queryInterface.bulkDelete('Users', null, {});
    }
};
2个回答

使用

sequelize db:seed:all

并分别向每个对象添加 createdDate {createdAt:'2016-03-31T08:00:10.354Z'} 和 updatedDate{updatedAt:'2016-03-31T08:00:10.354Z'}。

Shrikant
2016-03-31

试试这个方法

up: function (queryInterface, Sequelize) {
  console.log(User);
  return [
    queryInterface.bulkInsert('Users', [
      { username: "user1", createdAt: Date.now(), updatedAt: Date.now() },
      { username: "user2", createdAt: Date.now(), updatedAt: Date.now() }
    ])
  ];
}

或者可以使用 sequelize-fixtures 进行播种或插入测试数据

https://github.com/domasx2/sequelize-fixtures

Ashwini Jindal
2015-10-25