简体   繁体   English

异步等待函数返回未定义

[英]Async-Await function returns undefined

I'm finding myself with some inconvenient to return a certain result inside an async function which has an await request inside.我发现自己在异步函数中返回某个结果有些不方便,该函数内部有一个 await 请求。 I tried both 3 libraries (" http ", " request ", " then-request "), and it's always the same.我尝试了 3 个库(“ http ”、“ request ”、“ then-request ”),结果总是一样。

The main idea is that when the ajax ends, its result should be returned;主要思想是当ajax结束时,返回其结果; but instead, it returns undefined ( it doesn't respect the async/await ).但相反,它返回undefined它不尊重 async/await )。


File: index.server.js文件: index.server.js

const PyService = require("../../../api/services/PyService/validacionCSV");

module.exports = {
    pasajeClientes: async function (matcheos) {
        let resultado = await PyService.validate(matcheos);
        return resultado;
    }
}

File: validacionCSV.js文件: validacionCSV.js

const request = require('then-request');

module.exports = {

    validate: async (matcheos) => {

        var response;

        await request("GET", `${process.env.API_URL}/validate`, {
            json: {
                csv: {
                    clients: "datosPersonas.csv",
                    products: "movimientos.csv"
                },
                primary_keys: {
                    clients: "ID",
                    products: "ID",
                },
                branches: {
                    products: "rama",
                },
                rules: {
                    clients: matcheos["clientes"],
                    products: matcheos["productos"],
                }
            }
        }).done((resultado) => {

            let matched = resultado.ok;
            let no_relationships = resultado.no_relationships;
            let repeated = resultado.repeated;
            let total = resultado.total;
            let type_errors = resultado.type_errors;

            response = { 
                error: false,
                message: "",
                errorConTipoDatoClientes: type_errors.clients,
                errorConTipoDatoProductos: type_errors.products,
                errorConClientesSinProductos: no_relationships.clients,
                errorConProductosSinCliente: no_relationships.productos,
                errorConClientesRepetidos: repeated.clients,
                errorConProductosRepetidos: repeated.products,
                cantClientesOk: matched.clients,
                cantProductosOk: matched.products,
                cantClientesEnArchivo: total.clients,
                cantProductosEnArchivo: total.products,
            }

            if (no_relationships.clients > 0 || no_relationships.products > 0
            ||  repeated.clients > 0 || repeated.products > 0
            ||  type_errors.clients > 0 || type_errors.products > 0
            ) {
                response.error = true;
                response.message = "Los clientes/productos importados poseen errores."
            }
            else
                response.message = "Los clientes/productos importados no poseen errores."

        });

        return response;
    }
}

You are mixing Promise callbacks with async/await.您正在将 Promise 回调与 async/await 混合使用。 When working with callbacks you can't define a variable outside and then instantiate within the callback and then try to use it outside the call back again.使用回调时,您不能在外部定义变量,然后在回调中实例化,然后再次尝试在回调之外使用它。 Read more on Promises.阅读更多关于 Promise 的信息。

All I did was return response within the callback function.我所做的只是在回调函数中返回response

Try this尝试这个

 const request = require('then-request'); module.exports = { validate: async(matcheos) => { var response; await request("GET", `${process.env.API_URL}/validate`, { json: { csv: { clients: "datosPersonas.csv", products: "movimientos.csv" }, primary_keys: { clients: "ID", products: "ID", }, branches: { products: "rama", }, rules: { clients: matcheos["clientes"], products: matcheos["productos"], } } }).done((resultado) => { let matched = resultado.ok; let no_relationships = resultado.no_relationships; let repeated = resultado.repeated; let total = resultado.total; let type_errors = resultado.type_errors; response = { error: false, message: "", errorConTipoDatoClientes: type_errors.clients, errorConTipoDatoProductos: type_errors.products, errorConClientesSinProductos: no_relationships.clients, errorConProductosSinCliente: no_relationships.productos, errorConClientesRepetidos: repeated.clients, errorConProductosRepetidos: repeated.products, cantClientesOk: matched.clients, cantProductosOk: matched.products, cantClientesEnArchivo: total.clients, cantProductosEnArchivo: total.products, } if (no_relationships.clients > 0 || no_relationships.products > 0 || repeated.clients > 0 || repeated.products > 0 || type_errors.clients > 0 || type_errors.products > 0 ) { response.error = true; response.message = "Los clientes/productos importados poseen errores." } else response.message = "Los clientes/productos importados no poseen errores." return response }); } }

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

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