Nodeman app crashed ,not able to connect to database


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.set('view engine','hbs');

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;

        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!';

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

        case '3':
            message = 'Deleted Succesfully!';

})'/store_book',async (req,res)=>{
    let database = await dbo.getDatabase();
    const collection = database.collection('books');
    let book = { title: req.body.title, author:  };
    await collection.insertOne(book);
    return res.redirect('/?status=1');
})'/update_book/:edit_id',async (req,res)=>{
    let database = await dbo.getDatabase();
    const collection = database.collection('books');
    let book = { title: req.body.title, 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');})


Mongodb CRUD Example
<h1>Mongodb CRUD Example</h1>
{{#if edit_id}}
<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="{{}}">
    <input type="submit" >

<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" >


    {{#each books}}
    <li> {{this.title}} - {{}}  <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>

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.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.