javascript - 序列化错误处理

标签 javascript node.js sequelize.js

看一下下面的示例代码,我想知道在最外面的 Sequelize 任务上仅链接一个 .catch() 是否就足够了,而不是总是在每个任务上链接它,这看起来相当不错凌乱。

第二个问题是是否可以使 .then(instance, error) 包含错误对象(如果有)?就像 Mongoose 一样,第一个参数始终是 err 对象。所以我不必 .catch 错误,而只需在 .then() 回调 fn 中处理它。

function facebookStrategy() {
  return new FacebookStrategy({
    clientID: config.facebook.app_id,
    clientSecret: config.facebook.app_secret
  }, function (accessToken, refreshToken, profile, done) {
    process.nextTick(function () {
      models.User.findOrCreate({
        where: {email: profile.emails[0].value}
      }).spread(function (user, created) {
        models.UserPassport.findOrCreate({
          where: {
            // TODO(tsm): check out sequelize composite keys
            method: 'facebook',
            social_id: profile.id
          },
          defaults: {
            method: 'facebook',
            token: accessToken,
            social_id: profile.id
          }
        }).spread(function (userPassport, created) {
          userPassport.setUser(user).then(function () {
            user.updateAttributes({
              first_name: profile.name.givenName,
              last_name: profile.name.familyName
            }).then(function () {
              return done(null, user);
            });
          });
        }).catch(function (err) {
          console.log('Error occured: ', err);
          return done(err);
        });
      }).catch(function (err) {
        console.log('Error occured: ', err);
        return done(err);
      });
    })
  })
}

最佳答案

通过返回一个 Promise,你可以将它链接起来。

所以你可以做类似的事情

function facebookStrategy() {
    return new FacebookStrategy({
        clientID: config.facebook.app_id,
        clientSecret: config.facebook.app_secret
    }, function (accessToken, refreshToken, profile, done) {
        process.nextTick(function () {
            models.User.findOrCreate({
                where: {
                    email: profile.emails[0].value
                }
            }).spread(function (user, created) {
                return models.UserPassport.findOrCreate({
                    where: {
                        // TODO(tsm): check out sequelize composite keys
                        method: 'facebook',
                        social_id: profile.id
                    },
                    defaults: {
                        method: 'facebook',
                        token: accessToken,
                        social_id: profile.id
                    }
                }).spread(function (userPassport, created) {
                    return userPassport.setUser(user).then(function () {
                        return user.updateAttributes({
                            first_name: profile.name.givenName,
                            last_name: profile.name.familyName
                        }).then(function () {
                            return done(null, user);
                        });
                    });
                });
            }).catch(done);
        });
    });
}

关于javascript - 序列化错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30100514/

相关文章:

javascript简单替换引号内的所有内容

javascript - Node.js 中显示的 utf-8 字符不正确,如何解决?

heroku - 将 sequelize-cli 应用程序部署到 heroku DATABASE_URL 问题

javascript - 在页面加载时折叠导航栏

javascript - 无法在 Bootstrap 框架中打开可折叠菜单

javascript - 如果未设置 key ,则设置 key - 哪种方式最有效?

javascript - 保证异步js

javascript - 格式化数组以在 node.js 中输出

mysql - 如果我想优先显示某些条件,如何在 Sequelize for MySQL 中排序?

node.js - 在 Sequelize 中使用带有内部连接的组给出错误