sequelize - 无法读取未定义的 属性 捕获

sequelize - cannot read property catch of undefined

我在我的项目中使用 sequelize 和 mysql。当前的用例是关于用一些默认角色用 'roles_master' 填充 table。

函数 createDefaultRoles 实现检查:

我的代码如下...

/**
 *  Creates a pre-defined set of roles
 *  If roles table exists and already contains data, 
 *  this function simply returns, without creating any roles.
 *  The list of roles is defined in the file "ecp_db_defaults.js"
 *
 *  @param force -- boolean flag to indicate whether to drop existng roles and recreate afresh
 **/
function createDefaultRoles(force, callback) {
  console.log("Executing createDefaultRoles...");
  db.Role.count().then(function(result) {
    if (result>0) {
      if (force) {
        console.log("Emptying the roles table...");
        db.Role.destroy({force:true, where: {}}).then(function() {
          db.Role.bulkCreate(dbConfig.roles).done(function() {
            console.log("1. Successfully created user roles.");
            if (callback) return callback(null); else return;
          }).catch(function(e){
            console.log("Error while creating roles...", e);
            if (callback) return callback(e);
          });
        }).catch(function(e) {
          console.error("Error while destroying roles table.", e);
          return (callback)?callback(e):e;
        });
      }
      //if (callback) return callback(null); else return; 
    } else {
      db.Role.bulkCreate(dbConfig.roles).done(function(){
        console.log("2. Successfully created user roles.");
        if (callback) return callback(null);
      })
    }
  }).catch(function(e){
    console.error("Error while querying roles table.", e);
    if (callback) return callback(e);
  });
}

问题是,它部分起作用。虽然它能够删除现有行并添加新行,但它也会抛出一个异常,该异常被捕获并打印如下..

最终,由于上述错误,函数失败了。我已经尝试正确匹配所有 .then() 和 .catch() 承诺。他们好像还好。

非常感谢任何解决此问题的帮助。

我猜问题出在块

db.Role.bulkCreate(dbConfig.roles).done(function() {
            console.log("1. Successfully created user roles.");
            if (callback) return callback(null); else return;
          }).catch(function(e){
            console.log("Error while creating roles...", e);
            if (callback) return callback(e);
          });

由于 .done() returns 未定义,您不能在此附加 'catch'。 如果您将 .done 替换为 .then 它应该按预期工作