Answer the question
In order to leave comments, you need to log in
Export promise/async function (result) of node.js via module.exports?
Hey!
And so, a question that probably comes up with every new version of JS and almost everyone who has come across something asynchronous on node.js.
There is a module code (briefly):
function имя1 (параметр_из_формы) {
request (параметры)
.then(response => {
let charset = [];
charset.name = response.data.
//здесь код, который что-то делает
return (charset);
})
.then(charset => {
if (charset.value=== undefined) {
charset.value= 'PR10';
} else {
charset.guild = charset.guild.name;
}
return charset; //то, что мне надо получить в app.js
})
.catch(function (error) {
console.log('ошибка')
});
}
module.exports = имя1
const trade_log = require("./db/models/trade_log");
const имя1 = require("./db/ops/модуль");
app.all('/log', function (req, res, next) { //это у нас Express
let x = имя1(req.body.Counterparty); //вот здесь у меня проблема
trade_log.create({ //тут мы пишем в базу
Flag: req.body.Flag,
Instrument: req.body.Instrument,
Venue: req.body.Venue,
Price: req.body.Price,
Currency: req.body.Currency,
Quantity: req.body.Quantity,
Counterparty: req.body.Counterparty,
Identifier: x,
Commentary: req.body.Commentary,
},function (err, res) {
if (err) return console.log (x) + handleError(err);
console.log(res);
});
next();
});
app.all('/log', async function (req, res, next) { //это у нас Express
let x = await имя1(req.body.Counterparty)
Answer the question
In order to leave comments, you need to log in
Something like this
module.exports = async function имя1(params) {
try {
const response = await request(params);
let charset = [];
charset.name = response.data;
const charsetResult = await someFunc(charset);
if (charsetResult.value === undefined) {
charsetResult.value = 'PR10';
} else {
charsetResult.guild = charset.guild.name;
}
return charsetResult;
} catch (error) {
console.log(error);
};
}
It seems that everything is simple.
function имя1 (параметр_из_формы) {
return request (параметры) /*****/
}
let x = имя1(req.body.Counterparty)
.then(() => trade_log.create({/***/}));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question