Nodeman app crashed ,not able to connect to database

index.js

const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const exhbs = require('express-handlebars');
const dbo = require('./db');
const ObjectID = dbo.ObjectID;

app.engine('hbs',exhbs.engine({layoutsDir:'views/',defaultLayout:"main",extname:"hbs"}))
app.set('view engine','hbs');
app.set('views','views');
app.use(bodyparser.urlencoded({extended:true}));

app.get('/',async (req,res)=>{
    let database = await dbo.getDatabase();
    const collection = database.collection('books');
    const cursor = collection.find({})
    let books = await cursor.toArray();

    let message = '';
    let edit_id, edit_book;

    if(req.query.edit_id){
        edit_id = req.query.edit_id;
        edit_book = await collection.findOne({_id:ObjectID(edit_id)})
    }

    if (req.query.delete_id) {
        await collection.deleteOne({_id:ObjectID(req.query.delete_id)})
        return res.redirect('/?status=3');
    }
    
    switch (req.query.status) {
        case '1':
            message = 'Inserted Succesfully!';
            break;

        case '2':
            message = 'Updated Succesfully!';
            break;

        case '3':
            message = 'Deleted Succesfully!';
            break;
    
        default:
            break;
    }


    res.render('main',{message,books,edit_id,edit_book})
})

app.post('/store_book',async (req,res)=>{
    let database = await dbo.getDatabase();
    const collection = database.collection('books');
    let book = { title: req.body.title, author: req.body.author  };
    await collection.insertOne(book);
    return res.redirect('/?status=1');
})

app.post('/update_book/:edit_id',async (req,res)=>{
    let database = await dbo.getDatabase();
    const collection = database.collection('books');
    let book = { title: req.body.title, author: req.body.author  };
    let edit_id = req.params.edit_id;

    await collection.updateOne({_id:ObjectID(edit_id)},{$set:book});
    return res.redirect('/?status=2');
})

app.listen(8000,()=>{console.log('Listening to 8000 port');})

main.hbs

Mongodb CRUD Example
<h1>Mongodb CRUD Example</h1>
{{message}}
<br>
{{#if edit_id}}
 <h3>Edit</h3>
<form method="post" action="/update_book/{{edit_id}}">
    <label for="">Title</label>
    <input type="text" name="title" value="{{edit_book.title}}">
    <label for="">Author</label>
    <input type="text" name="author" value="{{edit_book.author}}">
    <input type="submit" >
</form><br><br>
{{else}}

<h3>Create</h3>
<form method="post" action="/store_book">
    <label for="">Title</label>
    <input type="text" name="title">
    <label for="">Author</label>
    <input type="text" name="author">
    <input type="submit" >
</form><br><br>

{{/if}}

<ul>
    {{#each books}}
    <li> {{this.title}} - {{this.author}}  <a href="/?edit_id={{this._id}}">Edit</a>  <a href="/?delete_id={{this._id}}" onclick="return confirm('Are you sure want to delete?')" >Delete</a></li>
    {{/each}}
</ul>

This file doesn’t exist but you’re trying to import it, hence crash


Edit: not directly related, but what version of Node are you using? If it’s 18.11 or higher (this is the current long-term support version, so you should be at least on that), then it has watch functionality built-in, you don’t need Nodemon. Just run the app like

node app.js --watch
1 Like


Yes DanCouper thankyou , i removed the const dbo line and saved it and debbuged again but i still got uncaught reference error(i attached the screenshot ), can i know how to overcome it

Have you stopped then restarted the server? It’s saying the err is here, so if you’ve deleted this line:

Need to save and restart.

If you remove this:

const dbo = require('./db');

You no longer have an identifier dbo and all the code that relies on it and what it contained will not work.

const ObjectID = dbo.ObjectID;
//...
let database = await dbo.getDatabase();

It is like removing a word from a sentence, the sentence no longer works.

My is very bright.

What is very bright? Even the word bright has lost its context.

You can’t just remove a necessary piece of the code and expect the rest of the code to still work.


If you are following a tutorial please link to it. If there is a link to a GitHub repo with some starting code link to that as well. You are missing some code it would seem.