Я довольно новичок в JS / NodeJs, и я только что узнал об асинхронных / обещаниях.Мой код ниже выполняет функцию дважды, хотя это не должно.Вот код и вывод с выделенной проблемой:
async function itemDetails(req, response) {
const item_number = parseInt(req.params.listingID, 10);
const theList = await quering(item_number);
console.log("LIST " + theList);
console.log("item_number " + item_number);
//theList = res.rows[0];
// define context data
const contextData = {
id: theList.id,
title: 'Listing\'s Details',
object: theList.object,
price: theList.price,
image: theList.image,
firstavail: theList.firstavail,
lastavail: theList.lastavail,
delivery: 'Delivery',
};
console.log(contextData);
return response.render('item_details', contextData);
}
async function quering(code2){
const listID = await listIdentification(code2);
return new Promise(resolve => {
pool.query('SELECT * FROM listings WHERE id = $1;', [listID], (err, res) =>
{
if (err) {
throw err;
} else {
const theList = res.rows[0];
resolve(theList);
}
});
});
}
async function listIdentification(code){
return new Promise((resolve, reject) => {
try {
const listingID = parseInt(code, 10);
console.log("check " + typeof(listingID) + listingID );
resolve(listingID);
} catch (error)
{
reject(error);
}
});
}
контрольный номер36
LIST [объект объекта]
item_number 36
{id: 36, название: 'Детали объявления', объект: 'aNYTHIGN',
цена: 2, изображение: 'asd.jpg', firstavail: '2018-01-01', lastavail:'2019-02-02', поставка: 'Доставка'}
проверка номераNaN // <--- здесь проблема </p>
Почему он снова работает?Он снова вызывает функцию listIdentification
, и я не могу понять, почему.