URL Shortener Microservice 1st 2nd tests not passing

I’m not sure why the second test doesn’t pass when I can actually send the information to MongoDB Atlas. I also don’t know why I can’t seem to redirect for the third test.

app.post('/api/shorturl', async (req, res) => {
  const { url } = req.body;
  dns.lookup(url, (err) => {
    if (err) {
      res.json({ error: 'invalid url' });
    }
  });
  const urlObj = { original_url: url, short_url: getRandomInt(1, 10000) };
  const urlDoc = new shortURL(urlObj);
  await urlDoc.save();
  res.json(urlObj);
});

app.get('/api/shorturl/:short_url', async (req, res) => {
  const { short_url } = req.params;
  await shortURL.findOne({ short_url }, (err, doc) => {
    if (err) {
      res.json(err);
    } else {
      const { original_url } = doc;
      res.redirect(doc.original_url);
    }
  });
});

I also get the following on glitch console

events.js:174
      throw er; // Unhandled 'error' event
      ^
TypeError: Cannot destructure property `original_url` of 'undefined' or 'null'.
at shortURL.findOne (/app/server.js:72:32)
    at process.nextTick (/rbd/pnpm-volume/d0c4d1d9-61bd-48ca-b27d-b134d6829c5c/node_modules/mongoose/lib/model.js:4845:18)
    at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
    at process.nextTick (/rbd/pnpm-volume/d0c4d1d9-61bd-48ca-b27d-b134d6829c5c/node_modules/mongoose/lib/model.js:4847:15)
    at process._tickCallback (internal/process/next_tick.js:61:11)

solution: Glitch :・゚✧

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: URL Shortener Microservice

Link to the challenge:

Any help would be appreciated, thank you.

Hello there,

One thing to note: dns.lookup expects only the hostname as the first argument.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.