Been stuck on the **URL Shortener** project forever. Need help

So the problem I have now is I"m unable to find matches with findOne in mongoose.

This is the question I asked on stackoverflow:

My full source code on glitch/gomix:

  1. Are you getting any errors?
  2. Why do you have the async keyword on your red function?
  3. Does the console output anything from your findOne callback?

I would advise you to create another project, ditch everything you don’t need and simply try to write something to the database and retrieve it.
Ideally you would do that on your own computer, so you can check mongodb’s files by yourself, independent of your fetch function. That will help you isolate the problem of database manipulation.

As for your particular solution, are you sure that let redurl =${}; returns the correct value?
Why don’t you try to rl.findOne({}, (error, data) => { console.log(data) first? That will return all values in your database, and you can check if your search parameters are correct.
Maybe your URL doesn’t exist in the database yet and that’s why your function doesn’t work. Your declaration is correct and should return values, the most likely scenario is that you don’t have any values to return.

Some general advice:
too many comments end up causing more confusion than helping, assume anyone reading your code already understands mongodb/node/whatever technologies you are using, only comment what is difficult to make clear with variable names and convention.
before asking a question like that try to clean up your code a little, if you’re going to use this commented code create another file and dump it all in there, it makes life easier for people looking at your code.
“async” is the default on Node, you don’t really need to use it before functions, assume everything is async.

I agree with stripping down technology you don’t need to use. Trying using express only app.get(’/:id’, (req, res) … like the person suggested on stack overflow instead of using koa and koa router.

Sorry for the late reply. You were right redurl was not right I corrected it now. I can’t believe I missed that. It still isn’t working though, I get the following error

Error: Can’t set headers after they are sent.
2:16 PM
at ServerResponse.setHeader (_http_outgoing.js:371:11)
2:16 PM
at Object.set (/app/node_modules/koa/lib/response.js:440:16)
2:16 PM
at Object.redirect (/app/node_modules/koa/lib/response.js:261:10)
2:16 PM
at Object.proto.(anonymous function) [as redirect] (/app/node_modules/delegates/index.js:40:31)
2:16 PM
Jump to
at url.findOne (/app/server.js:126:9)
2:16 PM
at model.Query. (/app/node_modules/mongoose/lib/model.js:3737:16)
2:16 PM
at /app/node_modules/kareem/index.js:277:21
2:16 PM
at /app/node_modules/kareem/index.js:131:16
2:16 PM
at _combinedTickCallback (internal/process/next_tick.js:73:7)
2:16 PM
at process._tickCallback (internal/process/next_tick.js:104:9)

I’ve checked my database and also returned just the data without any parameters as you suggested . It seems right. There’s a new entry being created with every url entered just as normal.

I appreciate the advice I don’t usually comment much I thought this will help me debug better. And correct me if I’m wrong I assumed the async keyword was mandatory with Koa 2 functions.

Even though correct values are being created in the database I cannot seem to match yet. Can you please help me understand what the above error means ? (and the reason for it)

Honest answer: I don’t know Express, I learnt Koa instead.

I’ve never used Koa, so i really can’t tell, sorry.

As for your error, it’s just as it said, whenever you send data using HTTP you have headers and a body, headers tell info about the body, the body is the data (in a nutshell).
What you’re doing is trying to change the data the information was already sent. As i mentioned i never used Koa, so i can’t be of much help as to why this is happening, sorry.
It seems to be a problem with async control flow, however.


If the type in the DB is of number you need to convert the query param to a number before the findOne

Hi this is solved now.