My API routes appear to work in the browser but when I run the tests I get : TypeError: Cannot read property 'original_url' of null. Please can you help me fix this.
When it comes to the redirect, I would console.log the data returned by findOne and go from there. I had a similar problem but can’t recall what it was exactly. To get around it I went for
Thank you for replying. I have tried to implement the suggestions you made but now I get TypeError: Cannot read property 'original_url' of undefined. The post route sends the JSON as expected but when redirecting I have this error.
I’ve allowed myself to add 2 console logs to see what is passed to your app.get.
Then, I manually tried to go to ‘/api/shorturl/12345’ as such entry doesn’t exist in your db.
I got the following error:
TypeError: Cannot read property ‘original_url’ of undefined
at /home/runner/boilerplate-project-urlshortener/server.js:89:38
at /home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/model.js:5068:18
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted ‘error’ event on Function instance at:
at /home/runner/boilerplate-project-urlshortener/node_modules/mongoose/lib/model.js:5070:15
at processTicksAndRejections (internal/process/task_queues.js:79:11)
[nodemon] app crashed - waiting for file changes before starting…
Your expectations are to see {"error":"invalid url"}.
This will never happen due to how Url.find works. At the moment, your code tells node to return {"error":"invalid url"} IF an error happens while searching the database for req.params.url. However this operation is successful, whether it returns an empty object or not. In case of /api/shorturl/12345 it returns an empty object, and therefore if (err) return res.json({ error: 'invalid url' });
is skipped. The error actually happens in your return statement because redirect is in fact trying to access an empty object.
To resolve this, you need to check whether or not Url.find() returned an empty object first. If the object is not empty, then you can return the redirect.
This also means, you can use Url.findOne(), rather than Url.find()
Thank you for your feedback, I have added an if statement to check for the data object and the code now redirects the original url as expected. However for some reason I still can’t pass the second and third tests. Is it necessary for the short_url to be a number?
The whole point of these challenges is that you do them yourself, with somewhat limited help. I can’t do this for you.
Asking questions is ok, however, you should make your queries as specific as possible with as much info to work with.
I suggest you take the same trouble shooting steps I did for every test that you can’t pass. Look at the test requirements, see what part of your code should handle that, add console.logs to see that is happening, run some tests and look at the errors.
Yes you are right - sorry I should have been more specific with my previous post.
I did some troubleshooting as you suggested and discovered that I needed to validate the url , in addition to using the dns.lookup for validating the domain part of the url. I have finally managed to pass all the tests. Thank you for your help