I believe node is not saving fast enough

I’m almost done:

https://frequent-tractor.glitch.me/

However, running into one last issue it seems.
‘’’
createAndSaveUrl(req.body.url);
Url.findOne({website:req.body.url},function(err,result)
{
res.send(result);
});
‘’’

I have my findOne right after my save that is sent to the user right after submission. But if you try adding a new URL:
https://frequent-tractor.glitch.me/api/shorturl/new

Often the findOne won’t show the document afterwards.

I think this is a speed issue. Because if right afterwards I make a new submission with the exact same details, findOne can find the old document with the same details. From my coding newbie perspective, it seems that the findOne query is running before it is finished saving.

Any ideas what I can do? I tried using setTimeout to run up the clock for a second or two but that seemed really cumbersome and I’m sure there are much better solutions out there. Thanks!

it s not a “speed” issue. Javascript, thus Node, is asynchronous by nature . You have to write your route this way router.route(‘yourRoute’).post(async () => {

await createAndSave
const result = await findOne({}, () => {

});

isNull(result) ? res.status(200).json(result) : res.status(404)
});

by default, if your query returns null alway send a business error (404)