【问题标题】:promise.then functions only works if defined insidepromise.then 函数仅在内部定义时才有效
【发布时间】:2020-04-07 11:15:11
【问题描述】:

我从承诺中无法真正理解一些东西。它曾经发生在我身上,也有回调。我不知道我没有看到什么。当我在“promise.then”中定义一个函数时,它可以正常工作。但是当我在外面用相同的参数定义相同的函数时,它说参数没有定义。这里发生了什么事?还有另一种方法可以让代码更简洁?

我发布了一个使用 express 和 axios npm 的代码,但我认为这不是问题。

app.get('/', function(req, res) {
    //enviamos un mensaje a auth
    axios
        .post('http://localhost:8081', {
            mensaje : 'Empiezo en api-rest.'
        })
        .then(function(response) {
            //Ahora tengo que enviar la respuesta a priv
            axios
                .post('http://localhost:8082', response.data)
                .then(function(responsePriv) {
                    console.log(responsePriv);
                })
                .catch(function(error) {
                    console.log(error);
                });
        })
        .catch(function(error) {
            console.log(error);
        });
});

第二个代码

app.get('/', function(req, res) {
    //enviamos un mensaje a auth
    axios
        .post('http://localhost:8081', {
            mensaje : 'Empiezo en api-rest.'
        })
        .then(respuestaDeAuth(response))
        .catch(function(error) {
            console.log(error);
        });
});

function respuestaDeAuth(response) {
    //Ahora tengo que enviar la respuesta a priv
    axios
        .post('http://localhost:8082', response.data)
        .then(function(responsePriv) {
            console.log(responsePriv);
        })
        .catch(function(error) {
            console.log(error);
        });
}

【问题讨论】:

    标签: node.js promise


    【解决方案1】:

    您不必传递响应,

    app.get('/', function(req, res) {
        //enviamos un mensaje a auth
        axios
            .post('http://localhost:8081', {
                mensaje : 'Empiezo en api-rest.'
            })
            .then(respuestaDeAuth) // <-- Don't call your callback. Just pass it
            .catch(function(error) {
                console.log(error);
            });
    });
    

    当您添加then(respuestDeAuth(response))respuestDeAuth 函数立即执行一个未定义的变量调用响应。这就是为什么它的 say 响应没有被定义。

    您可以通过在外部词法环境中声明一个变量来做一个小实验来理解这一点,例如const response = "Some data"。然后评论 axios 请求并尝试console.log()response。这次您不会看到错误,而是会看到 response 变量的值

    编辑

    如果你想添加res参数,

     app.get('/', function(req, res) {
            //enviamos un mensaje a auth
            axios
                .post('http://localhost:8081', {
                    mensaje : 'Empiezo en api-rest.'
                })
                .then(() => respuestaDeAuth(res))
                .catch(function(error) {
                    console.log(error);
                });
        });
    

    您唯一需要注意的是,不要调用回调函数。这里我使用了一个函数,并在该函数内部调用了 respuestaDeAuth() 和原始的 res 对象。

    【讨论】:

    • 哦,对了!!多么愚蠢的错误哈哈哈。如果我想在回调中使用“res”(app.get('/', function(req, res) 中的参数)怎么办?我该怎么做?谢谢你的回答
    • 您需要同时传递resresponse,即.then(response =&gt; respuestaDeAuth(res, response)) 并相应地调整respuestaDeAuth 的签名,我不建议这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2013-09-05
    • 2023-01-17
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多