Reputation: 113
I'm finding myself with some inconvenient to return a certain result inside an async function which has an await request inside. I tried both 3 libraries ("http", "request", "then-request"), and it's always the same.
The main idea is that when the ajax ends, its result should be returned; but instead, it returns undefined (it doesn't respect the async/await).
File: 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
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;
}
}
Upvotes: 0
Views: 333
Reputation: 338
You are mixing Promise callbacks with 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.
All I did was return response
within the callback function.
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
});
}
}
Upvotes: 1