如何将变量传递到外部范围中的then()回调函数?

时间:2022-12-08 19:38:49

I have an API receiving an request and doing some querys and validation.

我有一个API接收请求并进行一些查询和验证。

I want to use the response reference from the route inside the findUserPlayer function so I can send something to the client. The findUser function will return a data object that will be used on findUserPlayer.

我想使用findUserPlayer函数中路由的响应引用,这样我就可以向客户端发送一些东西。findUser函数将返回一个将在findUserPlayer上使用的数据对象。

var api = express.Router();

api.post('/signup', function (request, response) {

    UserModel.find(req.body)
        .then(findUser, createUser)
        .then(findUserPlayer, createUserPlayer);
});

var findUserPlayer = function (data, res) {
    //...
    res.json(object);
};
  • I've tried calling findUserPlayer(data, res), which gives me 'data is not defined'.
  • 我试过调用findUserPlayer(data, res),它给出了“数据未定义”。
  • I've tried calling findUserPlayer(res), which will override data.
  • 我尝试调用findUserPlayer(res),它将覆盖数据。

How can I use the response object inside that callback?

如何在回调中使用响应对象?

3 个解决方案

#1


1  

Instead of directly passing findUserPlayer function as a success handler, you can define a success handler function while will then call findUserPlayer.

与直接将findUserPlayer函数作为成功处理程序传递不同,您可以定义一个成功处理程序函数,然后调用findUserPlayer。

var api = express.Router();

api.post('/signup', function (request, response) {

    UserModel.find(req.body)
        .then(findUser, createUser)
        .then(function(data) {
            return findUserPlayer(data, response);
        }, createUserPlayer);
});

var findUserPlayer = function (data, res) {
    //...
    res.json(object);
};

#2


1  

JavaScript is a very flexible language which means there are many ways you can handle this, one of the easiest (in terms of minimum code change) is to reverse the order of arguments in findUserPlayer and binding your context (.bind(response)).

JavaScript是一种非常灵活的语言,这意味着有很多方法可以处理这个问题,最简单的方法之一(就最低的代码更改而言)是逆转findUserPlayer中的参数顺序并绑定上下文(.bind(response))。

But that would be a poor design choice. There's no reason for a function that "finds a user" to deal with responses or json. It's out of this functions scope and responsibility. The request handler is the one that should deal with formatting a relevant response, and I'd do that by returning the relevant data in the promise chain and eventually:

但这将是一个糟糕的设计选择。没有理由“找到一个用户”来处理响应或json。它超出了功能范围和职责范围。请求处理程序是处理格式化相关响应的处理程序,我将通过返回承诺链中的相关数据来做到这一点,并最终:

.then(function(data) {
   response.json(data);
});

#3


1  

I'm a big fan of lodash and sometimes to a fault, but thought I would add the following using lodash's partialRight function.

我是lodash的超级粉丝,有时还会出错,但我想我应该使用lodash的partiright函数来添加以下内容。

var _ = require('lodash');
...
UserModel.find(req.body)
    .then(findUser, createUser)
    .then(_.partialRight(findUserPlayer, response), createUserPlayer);

_.partialRight returns a new function with the partials appended to the new function. The first argument of _.partialRight is the function we want to append the arguments to. The subsequent arguments are the args we want to append to the function.

_。partiright返回一个新函数,并将部分附加到新函数。_的第一个参数。partiright是我们想要添加参数的函数。后续的参数是我们想要添加到函数的args。

I'm assuming the following function signature: findUserPlayer(data, response). _.partialRight appends the response object to the new function. The new function signature looks like this now: function (data).

我假设有以下函数签名:findUserPlayer(data, response)。_。partiright会将响应对象附加到新函数。新的函数签名如下所示:function (data)。

documentation

文档

#1


1  

Instead of directly passing findUserPlayer function as a success handler, you can define a success handler function while will then call findUserPlayer.

与直接将findUserPlayer函数作为成功处理程序传递不同,您可以定义一个成功处理程序函数,然后调用findUserPlayer。

var api = express.Router();

api.post('/signup', function (request, response) {

    UserModel.find(req.body)
        .then(findUser, createUser)
        .then(function(data) {
            return findUserPlayer(data, response);
        }, createUserPlayer);
});

var findUserPlayer = function (data, res) {
    //...
    res.json(object);
};

#2


1  

JavaScript is a very flexible language which means there are many ways you can handle this, one of the easiest (in terms of minimum code change) is to reverse the order of arguments in findUserPlayer and binding your context (.bind(response)).

JavaScript是一种非常灵活的语言,这意味着有很多方法可以处理这个问题,最简单的方法之一(就最低的代码更改而言)是逆转findUserPlayer中的参数顺序并绑定上下文(.bind(response))。

But that would be a poor design choice. There's no reason for a function that "finds a user" to deal with responses or json. It's out of this functions scope and responsibility. The request handler is the one that should deal with formatting a relevant response, and I'd do that by returning the relevant data in the promise chain and eventually:

但这将是一个糟糕的设计选择。没有理由“找到一个用户”来处理响应或json。它超出了功能范围和职责范围。请求处理程序是处理格式化相关响应的处理程序,我将通过返回承诺链中的相关数据来做到这一点,并最终:

.then(function(data) {
   response.json(data);
});

#3


1  

I'm a big fan of lodash and sometimes to a fault, but thought I would add the following using lodash's partialRight function.

我是lodash的超级粉丝,有时还会出错,但我想我应该使用lodash的partiright函数来添加以下内容。

var _ = require('lodash');
...
UserModel.find(req.body)
    .then(findUser, createUser)
    .then(_.partialRight(findUserPlayer, response), createUserPlayer);

_.partialRight returns a new function with the partials appended to the new function. The first argument of _.partialRight is the function we want to append the arguments to. The subsequent arguments are the args we want to append to the function.

_。partiright返回一个新函数,并将部分附加到新函数。_的第一个参数。partiright是我们想要添加参数的函数。后续的参数是我们想要添加到函数的args。

I'm assuming the following function signature: findUserPlayer(data, response). _.partialRight appends the response object to the new function. The new function signature looks like this now: function (data).

我假设有以下函数签名:findUserPlayer(data, response)。_。partiright会将响应对象附加到新函数。新的函数签名如下所示:function (data)。

documentation

文档