Sails.js Production Environment不会在badRequest上返回JSON响应,但dev环境会返回

时间:2015-06-15 15:30:49

标签: node.js sails.js

如标题中所述,我的sails app对特定路由/控制器函数的GET请求在开发环境中使用JSON返回badRequest,但不在prod环境中返回。这是为什么?

这是控制器功能:

    index: function(req, res) {

    async.auto({

        companies: function(cb) {

            User.findOneById(req.session.user.id)
                 .populate('companies')
                 .exec(function(err, user) 
            {

                if(err) {
                    var badRequestData = { error: err };
                    return cb(badRequestData, null);
                } else if(user.companies.length == 0) {
                    var badRequestData = { error: "This user has no associated companies." };
                    return cb(badRequestData, null);
                }

                cb(null, user.companies)

            });

        },

        validateForNullCompanies: ['companies', function(cb, results) {

            var nullCompanies = _.where(results.companies, { stripeAccountId: null });

            if(nullCompanies.length > 0) {
                var badRequestData = { error: "This user needs to authenticate stripe with their company." };
                return cb(badRequestData, null);
            } else {
                return cb();
            }

        }]

    }, function(err, results) {

        if (err) {
            return res.badRequest(err);
        }

        return res.ok();

    });

},

2 个答案:

答案 0 :(得分:3)

类似于finnergizer提供的答案,如果你检查api / responses / badRequest.js,实际的代码应该是这样的:

  // Only include errors in response if application environment
  // is not set to 'production'.  In production, we shouldn't
  // send back any identifying information about errors.
  if (sails.config.environment === 'production' && sails.config.keepResponseErrors !== true) {
  data = undefined;
  }

评论中已经说明的原因。如果您仍然认为要在生产环境中显示错误消息,而不是注释掉此行,实际上您可以添加项目' keepResponseErrors'并在config / env / production.js中将其设置为true。像这样:

module.exports = {
  keepResponseErrors: true
};

通过这种方式,无需更改每个api / response js文件。

答案 1 :(得分:0)

我遇到了同样的问题并通过在 config/env/production.js 中添加以下代码解决

module.exports = {
   keepResponseErrors: true
}