Why does async / await function run twice?
up vote
1
down vote
favorite
I'm working with express and node.js application. I have a regular router, which:
- finds
myIDinDB, - if
myIDexists it tries toaddVisit()
- if error occures (may such table doesn't exist) it catches the error, creates the new table and
addVisit()to it.
The router code is below:
router.get('/:myId', errorHandler(async (req, res, next) => {
console.log(req.params.myId + ' This is myID Params!');
const Domain = DomainModel(db, Sequelize);
const Click = ClickModel(db, `${req.params.myId}_clicks`, Sequelize);
try {
let row = await Domain.findOne({ where: { myId: req.params.myId } });
console.log(row.myId + ' This is myID from DB');
if (row.myId) {
try {
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(`This error fires!`);
if (err.message !== `Validation error`) {
try {
await db.sync();
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(err);
res.sendStatus(400);
}
} else {
res.sendStatus(400);
}
}
} else {
console.log(`No such myId in use`);
res.sendStatus(400);
}
} catch (err) {
console.log(err);
}
})
But the code runs twice! My console.log() shows:
1. ==> 1231231231 This is myID Params!
2. ==> 1231231231 This is myID from DB
3. ==> This error fires!
4. ==> favicon.ico This is myID Params!
5. ==> TypeError: Cannot read property 'myID' of null
If I comment this part of code it runs only one time!
// if (err.message !== `Validation error`) {
// try {
// await db.sync();
// await addVisit(Click, req);
// res.sendStatus(200);
// } catch (err) {
// console.log(err);
// res.sendStatus(400);
// }
// } else {
// res.sendStatus(400);
// }
This is the only place where I use console.log()! This is the simple sample project in which I able to reproduce the behavior. So why does the code runs twice and WHY my params.myID became favicon.ico?
javascript node.js express async-await sequelize.js
add a comment |
up vote
1
down vote
favorite
I'm working with express and node.js application. I have a regular router, which:
- finds
myIDinDB, - if
myIDexists it tries toaddVisit()
- if error occures (may such table doesn't exist) it catches the error, creates the new table and
addVisit()to it.
The router code is below:
router.get('/:myId', errorHandler(async (req, res, next) => {
console.log(req.params.myId + ' This is myID Params!');
const Domain = DomainModel(db, Sequelize);
const Click = ClickModel(db, `${req.params.myId}_clicks`, Sequelize);
try {
let row = await Domain.findOne({ where: { myId: req.params.myId } });
console.log(row.myId + ' This is myID from DB');
if (row.myId) {
try {
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(`This error fires!`);
if (err.message !== `Validation error`) {
try {
await db.sync();
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(err);
res.sendStatus(400);
}
} else {
res.sendStatus(400);
}
}
} else {
console.log(`No such myId in use`);
res.sendStatus(400);
}
} catch (err) {
console.log(err);
}
})
But the code runs twice! My console.log() shows:
1. ==> 1231231231 This is myID Params!
2. ==> 1231231231 This is myID from DB
3. ==> This error fires!
4. ==> favicon.ico This is myID Params!
5. ==> TypeError: Cannot read property 'myID' of null
If I comment this part of code it runs only one time!
// if (err.message !== `Validation error`) {
// try {
// await db.sync();
// await addVisit(Click, req);
// res.sendStatus(200);
// } catch (err) {
// console.log(err);
// res.sendStatus(400);
// }
// } else {
// res.sendStatus(400);
// }
This is the only place where I use console.log()! This is the simple sample project in which I able to reproduce the behavior. So why does the code runs twice and WHY my params.myID became favicon.ico?
javascript node.js express async-await sequelize.js
1
Because your browser is sending two requests.
– tkausl
2 days ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working with express and node.js application. I have a regular router, which:
- finds
myIDinDB, - if
myIDexists it tries toaddVisit()
- if error occures (may such table doesn't exist) it catches the error, creates the new table and
addVisit()to it.
The router code is below:
router.get('/:myId', errorHandler(async (req, res, next) => {
console.log(req.params.myId + ' This is myID Params!');
const Domain = DomainModel(db, Sequelize);
const Click = ClickModel(db, `${req.params.myId}_clicks`, Sequelize);
try {
let row = await Domain.findOne({ where: { myId: req.params.myId } });
console.log(row.myId + ' This is myID from DB');
if (row.myId) {
try {
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(`This error fires!`);
if (err.message !== `Validation error`) {
try {
await db.sync();
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(err);
res.sendStatus(400);
}
} else {
res.sendStatus(400);
}
}
} else {
console.log(`No such myId in use`);
res.sendStatus(400);
}
} catch (err) {
console.log(err);
}
})
But the code runs twice! My console.log() shows:
1. ==> 1231231231 This is myID Params!
2. ==> 1231231231 This is myID from DB
3. ==> This error fires!
4. ==> favicon.ico This is myID Params!
5. ==> TypeError: Cannot read property 'myID' of null
If I comment this part of code it runs only one time!
// if (err.message !== `Validation error`) {
// try {
// await db.sync();
// await addVisit(Click, req);
// res.sendStatus(200);
// } catch (err) {
// console.log(err);
// res.sendStatus(400);
// }
// } else {
// res.sendStatus(400);
// }
This is the only place where I use console.log()! This is the simple sample project in which I able to reproduce the behavior. So why does the code runs twice and WHY my params.myID became favicon.ico?
javascript node.js express async-await sequelize.js
I'm working with express and node.js application. I have a regular router, which:
- finds
myIDinDB, - if
myIDexists it tries toaddVisit()
- if error occures (may such table doesn't exist) it catches the error, creates the new table and
addVisit()to it.
The router code is below:
router.get('/:myId', errorHandler(async (req, res, next) => {
console.log(req.params.myId + ' This is myID Params!');
const Domain = DomainModel(db, Sequelize);
const Click = ClickModel(db, `${req.params.myId}_clicks`, Sequelize);
try {
let row = await Domain.findOne({ where: { myId: req.params.myId } });
console.log(row.myId + ' This is myID from DB');
if (row.myId) {
try {
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(`This error fires!`);
if (err.message !== `Validation error`) {
try {
await db.sync();
await addVisit(Click, req);
res.sendStatus(200);
} catch (err) {
console.log(err);
res.sendStatus(400);
}
} else {
res.sendStatus(400);
}
}
} else {
console.log(`No such myId in use`);
res.sendStatus(400);
}
} catch (err) {
console.log(err);
}
})
But the code runs twice! My console.log() shows:
1. ==> 1231231231 This is myID Params!
2. ==> 1231231231 This is myID from DB
3. ==> This error fires!
4. ==> favicon.ico This is myID Params!
5. ==> TypeError: Cannot read property 'myID' of null
If I comment this part of code it runs only one time!
// if (err.message !== `Validation error`) {
// try {
// await db.sync();
// await addVisit(Click, req);
// res.sendStatus(200);
// } catch (err) {
// console.log(err);
// res.sendStatus(400);
// }
// } else {
// res.sendStatus(400);
// }
This is the only place where I use console.log()! This is the simple sample project in which I able to reproduce the behavior. So why does the code runs twice and WHY my params.myID became favicon.ico?
javascript node.js express async-await sequelize.js
javascript node.js express async-await sequelize.js
asked 2 days ago
Nastro
1339
1339
1
Because your browser is sending two requests.
– tkausl
2 days ago
add a comment |
1
Because your browser is sending two requests.
– tkausl
2 days ago
1
1
Because your browser is sending two requests.
– tkausl
2 days ago
Because your browser is sending two requests.
– tkausl
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Whenever you open a webpage, there is a small icon in the upper left corner of the tab, containing the logo of the page. That icon gets loaded from theserver/favicon.ico, if that returns an error the icon stays empty, otherwise the photo returned by that is used as the icon. There are other such reserved files, such as norobots.txt and manifest.json. Therefore it is a bad idea to have the variable url part at the main level. This one:
router.get('/:myId'
catches everything. Instead you should move it to a subpath:
router.get('/id/:myId'
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
add a comment |
up vote
2
down vote
Because with route param /:myId you will serve /favicon request too. So those are the 2 requests.
What should I do to serv only one request?
– Nastro
2 days ago
1
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part./static-part/:my-paramOr you can validate your url/param with some RegExp, but that is very expensive.
– lependu
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Whenever you open a webpage, there is a small icon in the upper left corner of the tab, containing the logo of the page. That icon gets loaded from theserver/favicon.ico, if that returns an error the icon stays empty, otherwise the photo returned by that is used as the icon. There are other such reserved files, such as norobots.txt and manifest.json. Therefore it is a bad idea to have the variable url part at the main level. This one:
router.get('/:myId'
catches everything. Instead you should move it to a subpath:
router.get('/id/:myId'
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
add a comment |
up vote
4
down vote
accepted
Whenever you open a webpage, there is a small icon in the upper left corner of the tab, containing the logo of the page. That icon gets loaded from theserver/favicon.ico, if that returns an error the icon stays empty, otherwise the photo returned by that is used as the icon. There are other such reserved files, such as norobots.txt and manifest.json. Therefore it is a bad idea to have the variable url part at the main level. This one:
router.get('/:myId'
catches everything. Instead you should move it to a subpath:
router.get('/id/:myId'
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Whenever you open a webpage, there is a small icon in the upper left corner of the tab, containing the logo of the page. That icon gets loaded from theserver/favicon.ico, if that returns an error the icon stays empty, otherwise the photo returned by that is used as the icon. There are other such reserved files, such as norobots.txt and manifest.json. Therefore it is a bad idea to have the variable url part at the main level. This one:
router.get('/:myId'
catches everything. Instead you should move it to a subpath:
router.get('/id/:myId'
Whenever you open a webpage, there is a small icon in the upper left corner of the tab, containing the logo of the page. That icon gets loaded from theserver/favicon.ico, if that returns an error the icon stays empty, otherwise the photo returned by that is used as the icon. There are other such reserved files, such as norobots.txt and manifest.json. Therefore it is a bad idea to have the variable url part at the main level. This one:
router.get('/:myId'
catches everything. Instead you should move it to a subpath:
router.get('/id/:myId'
answered 2 days ago
Jonas Wilms
51.6k42344
51.6k42344
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
add a comment |
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
OMG! How much time I lose with this (( Thank you for explanation. Now it works like it should!
– Nastro
2 days ago
add a comment |
up vote
2
down vote
Because with route param /:myId you will serve /favicon request too. So those are the 2 requests.
What should I do to serv only one request?
– Nastro
2 days ago
1
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part./static-part/:my-paramOr you can validate your url/param with some RegExp, but that is very expensive.
– lependu
2 days ago
add a comment |
up vote
2
down vote
Because with route param /:myId you will serve /favicon request too. So those are the 2 requests.
What should I do to serv only one request?
– Nastro
2 days ago
1
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part./static-part/:my-paramOr you can validate your url/param with some RegExp, but that is very expensive.
– lependu
2 days ago
add a comment |
up vote
2
down vote
up vote
2
down vote
Because with route param /:myId you will serve /favicon request too. So those are the 2 requests.
Because with route param /:myId you will serve /favicon request too. So those are the 2 requests.
answered 2 days ago
lependu
329110
329110
What should I do to serv only one request?
– Nastro
2 days ago
1
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part./static-part/:my-paramOr you can validate your url/param with some RegExp, but that is very expensive.
– lependu
2 days ago
add a comment |
What should I do to serv only one request?
– Nastro
2 days ago
1
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part./static-part/:my-paramOr you can validate your url/param with some RegExp, but that is very expensive.
– lependu
2 days ago
What should I do to serv only one request?
– Nastro
2 days ago
What should I do to serv only one request?
– Nastro
2 days ago
1
1
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part.
/static-part/:my-param Or you can validate your url/param with some RegExp, but that is very expensive.– lependu
2 days ago
It is bad a practice to use param in the first place. Because you will serve every request. You should always start your route with static part.
/static-part/:my-param Or you can validate your url/param with some RegExp, but that is very expensive.– lependu
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238192%2fwhy-does-async-await-function-run-twice%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Because your browser is sending two requests.
– tkausl
2 days ago