Tell us what’s happening:
It is the first challenge I try to complete on my own with Mongoose.
I can’t find the reason why the app crashes every time I try to access the Url model in my database.
It always claims that it tries to access properties of null!
Part of the log:
Just follow the error message. It’s telling you it can’t read a property short_url of an object and there is only one place such a call is made:
app.post('/api/shorturl', bodyParser.urlencoded({extended: false}),(req, res)=>{
const fullUrl = req.body.url;
let shortUrl = 1;
Url.findOne({}) // This is not the find you are looking for.
.sort({short_url:'desc'})
.exec((error, result)=>{
if (!error && result !== undefined) {
// Here's the problem. Log some stuff here.
shortUrl = result.short_url + 1;
}
What happens with shortUrl if you are creating the first record and result is empty (empty is not undefined)? Read this to see what happens when you send an empty object to findOne().
Also please post errors (code, etc.) as code blocks. Much more useful than an image. Posting the repl.it was very helpful.