简体   繁体   English

将参数传递给异步/等待 function 返回“未定义”

[英]Passing argument to async / await function returns "undefined"

When posting data to an API and get a response, if I hardcode the body data inside the fetch call (body: "XYZ12345") it works fine, this is an example:将数据发布到 API 并获得响应时,如果我在获取调用(正文:“XYZ12345”)中硬编码正文数据,它工作正常,这是一个示例:

            const vatValidationRequest =
                fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: "XYZ12345",
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });


            const validateVAT = async () => {
                const viesResponse = await vatValidationRequest;
                console.log(viesResponse);
            };

            validateVAT();

However, if I try to pass the body data as an argument (body: vatNumber), the validateVAT() function returns "undefined".但是,如果我尝试将正文数据作为参数(正文:vatNumber)传递,validateVAT() function 将返回“undefined”。 This is what's not working:这是行不通的:

            const vatValidationRequest = (vatNumber) => {
                fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: vatNumber,
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });
            }


            const validateVAT = async (vatNumber) => {
                const viesResponse = await vatValidationRequest(vatNumber);
                console.log(viesResponse);
            };

            validateVAT("XYZ12345");

Any clues about how to pass the argument to the async function?关于如何将参数传递给异步 function 的任何线索? thanks!谢谢!

The problem is that you are not returning the response from the method.问题是您没有从该方法返回响应。 You should do this:你应该做这个:

            const vatValidationRequest = (vatNumber) => {
                return fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: vatNumber,
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });
            }


            const validateVAT = async (vatNumber) => {
                const viesResponse = await vatValidationRequest(vatNumber);
                console.log(viesResponse);
            };

            validateVAT("XYZ12345");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM