Url Shortner Redirect test fails but manually works

Tell us what’s happening:
Hey,
I’ve been doing the urlshortner project and finished it, all tests passed execpt for post and redirect.

I’ve logged the api req and saw that its requesting the path:
“/api/shorturl/undefined”

image

I’ve verified that the json was posting correctly by forcing some fixed value, the test still requests /api/shorturl/undefined

I’ve went to the FCC github and pulled the test and ran it in JSFiddle, and access is done correctly!
https://jsfiddle.net/qv4ouegj/3/

Your code so far

Your browser information:

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

Challenge: URL Shortener Microservice

bump, any ideas to what could causing this?

Your own logging is telling you the problem:

GET /api/shorturl/undefined - ::ffff:172.18.0.1
{ shortId: 'undefined' }

The shortId being provided is undefined. Since that is obtained from your API by sending in a URL, your API is providing a value of undefined for the test URL for the failing test. So the problem is in your POST route. I forked your project and added more logging of (all) route inputs and (all) responses and the problem is in your URL validation or duplication prevention. Since the test URLs are the same except for a parameter, I believe your validation code is detecting that the protocol/host is the same and flagging the URL as identical to a previous one and hence, not sending a response with a valid shortId to use. You really need to log all route inputs and responses even when debugging one route because the routes interact via the database and the tests use one route to test the other.

You also need to return your responses (return res.json({...}); not res.json({...})).

Hey Jeremy,
Thanks for the help

The issue ended up being the regular expression which validates the url structure, since I used the ‘g’ flag it would concat any string passed to the regex and thus chain both POST commands and thus saying the second url is invalid, removing the flag made it pass all the tests :slight_smile:

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