Похожие чаты

Hi guys, I have a problem with Promise rejection. I'm using

the latest nodejs LTS version and MySQL package.

I wrote a module like the following:
[..]
getUserById: (userid) => {
return new Promise((resolve, reject) => {
db.getConnection((err, connection) => {
if (err) {
return reject(err);
}

let sql = 'SELECT * FROM users WHERE userid = ?';
let param = [userid];
sql = mysql.format(sql, param);

connection.query(sql, (err, rows) => {
if (err) {
return reject(err);
}

resolve(rows);
connection.release();
});
});
});
}
[..]
and calling it in route file:
[..]
const User = require('../models/user_model').CustomQueries;

router.get('/users/:userid', (req, res, next) => {
User
.getUserById(req.params.userid)
.then((rows) => {
if (rows.length === 0) {
res.json({ message: 'No user found.' });
}
res.status(200).json(rows);
})
.catch((err) => {
res.json(err);
});
});
When i try to get users with invalid userid i always get a warning
(node:7809) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Can't set headers after they are sent.
(node:7809) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.Did i write a wrong Promise?

2 ответов

6 просмотров

Yes. Just remove the .status(200) part

Oh, I see, you're calling res.json twice

Похожие вопросы

Обсуждают сегодня

Какой-то там пердун в 90-х решил, что есть какая-то разная типизация. Кого вообще это волнует?
КТ315
49
Hi. Do we have a raid bot? Why nobody doing raids on X? Even RH mentioned this and nobody paying attention...whats the channel for hex memes? If mods cant run raids just insta...
H
31
Подскажите, а есть vault lite или ченить такое?) А то нужен вольт для похода в вольт, но весит он ~500 мб) как-то многовато для парочки запросов ))
Alexandr Orloff
17
блеать, почему так?? где в роутере это исправляется?
Арсен Маньяков 🇦🇲
16
void terminal_scroll() { memmove(terminal_buffer, terminal_buffer + VGA_WIDTH, buffer_size - VGA_WIDTH); memset(terminal_buffer + buffer_size - VGA_WIDTH, 0, VGA_WIDTH); ...
Егор
47
🌊 Ocean Nodes Dashboard Update 🚀 Hey, Oceaners! First off, a massive round of applause 👏to all of you for the amazing engagement since we launched Ocean Nodes. In just a few ...
KreigDK | Never DM first🌊
3
Всем привет! Подскажите, пожалуйста, в чем ошибка? Настраиваю подключение к MySQL. Либы лежат рядом с exe. Все как по "учебнику"
Евгений
16
А можете как-то проверить меня по знаниям по ассемблеру?
A A
132
Здравствуйте! У меня появилась возможность купить книгу "Изучай Haskell во имя добра!". Но я где-то слышал, что эта книга устарела. Насколько это правда??
E
22
люди, которые используют flameshot, к вам вопрос. Можно-ли поставить хоткей на создание скриншота? В программе есть отдел "горячие клавиши", но там все для редактирования, скр...
ThunDer104
11
Карта сайта