Can't finish URL Shortener checklist

Hello guys, I have already finish up my code and it has been tested several times and it works with all cases given in Shorten Url case. But I can’t finish it. Every time I validate it says an error:

CastError: Cast to Number failed for value “undefined” (type string) at path “short_url” for model “Shortener”

I don’t know what’s wrong because there is no clue about storing the ‘undefined’ result to db in case the short url is nothing on the database. Can anyone please help me? Thank you!

Your project link(s)

solution: boilerplate-project-urlshortener - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0

Challenge: URL Shortener Microservice

Link to the challenge:

You are not handling the URL validation correctly.

This is an example of what the valid URL used might look like:

https://boilerplate-project-urlshortener.mdzul17.repl.co/?v=1654388406420

Log out req.body.url so you know what you are working with. You can also inspect the payload using the network tab.

Okay I get it now for this case. Thank you before! Now I have fix my code.

But, I still doesn’t know why the checklist for json response case and redirect case still won’t finish. It tells me about the short_url value is input by undefined, meanwhile in my code there is no code that put the ‘undefined’ value to short_url. Can you give me any suggestion for this case? Thanks!

Did you update your Replit? I don’t see a code change that would allow the valid URL. You are responding with {"error":"invalid url"} for the valid URL.


The easiest way to pass this is to just check for the invalid URL that is passed and just assume everything else is valid (a bit of a cheat I guess).

But you can use dns.lookup as suggested in the challenge (and even use fetch to do link checking).

https://nodejs.org/api/dns.html#dnslookuphostname-options-callback

Yes, I have done with the case ‘invalid url’, but for the case for

You can POST a URL to /api/shorturl and get a JSON response with original_url and short_url properties. Here’s an example: { original_url : 'https://freeCodeCamp.org', short_url : 1}

and

When you visit /api/shorturl/<short_url>, you will be redirected to the original URL.

it still not finished. But when I try to run my code and set the short url for parameter, it can redirect to the original url I saved before. Instead, in the replit there is an error

CastError: Cast to Number failed for value “undefined” (type string) at path “short_url” for model “Shortener”

I mean, I think there is none of my code handling to put the ‘undefined’ value to the db, but the system still want to try to put it and the error appear. Is it correct? Or maybe I have some misunderstanding about the error?

You are still not validating URLs correctly. Your regex will match the valid URL and res {"error":"invalid url"} and then return. I’d suggest you give dns.lookup a read and/or fix the regex.

OH MY GOD, I FINALLY DID IT!

My bad for not being focused on the point about dns.lookup . I thought my regex is just being fine because it runs well in the database. So as you suggest, I replace the regex code and use the dns.lookup instead, and it was solved! Thank you and sorry if this bothers you :laughing: Thank you so much btw!