TypeError: Cannot read property 'json' of undefined

Hi there!

I have been struggling with “TypeError: Cannot read property ‘json’ of undefined”. I’d like to flatten (the nested function) below

        mongodbDocument.save((err, data) => {
          if (err) return console.error(err);
          res.json({
            original_url: data.original_url,
            short_url: data.short_url
          });
        });

with

const saveAndRespond = (doc) => {
  doc.save((err, data, res) => {
    if (err) return console.error(err);
    res.json({
      original_url: data.original_url,
      short_url: data.short_url
    });
  });
};

Here is a link: https://glitch.com/edit/#!/mountainous-fearless-shrimp

I would mean a lot to me if you could help me!

/Mattias

Can you please say what is the exact error? Usually they give a line and that can help.

The logs says:

events.js:174

throw er; // Unhandled ‘error’ event
^

TypeError: Cannot read property ‘json’ of undefined
at doc.save (/app/server.js:60:9)

Then there are more subsequent error messages.

Just go to line 60 of server.js