bradrar
bradrar

Reputation: 737

Mongoose is not working with hapi.js(simple setup)

I am experimenting with hapi.js and want to connect hapi with mongoose. but whenever I go to the route, it is saying Internal Server Error

quick setup:

my file structure is this: (I didn't include non related stuff to the problem)

/app.js
/node_modules
/models
    /Product.js
/routes
    /product-page
         /product.js
/views
    /product
         /product.html

inside my app.js is this.

const Hapi = require('hapi');

const routes = require('./routes');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/hapidb', { useNewUrlParser: true })
    .then(() => console.log('MongoDB connected...'))
    .catch(err => console.error(err));



const server = Hapi.server({
    port: 3000,
    host: 'localhost'
});


const init = async () => {

    await server.start();
    console.log(`Server running at: ${server.info.uri}`);

    // Home Route
    server.route(routes);

    await server.register(require('vision'));

    server.views({
        engines: {
            html: require('handlebars')
        },
        relativeTo: __dirname,
        path: 'views',
        partialsPath: 'views/partials'
    });


};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

as you can see, mongoose.connect is working and when I type node app.js in the terminal, MongoDB connected... is getting the output.

Now I created a models/Product.js for the schema and this is the schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var ProductSchema = new Schema({
    name: String
});
module.exports = mongoose.model('Product', ProductSchema);

and now inside my routes product.js file. This is the setup:

    const Product = require('../../model/Product')


module.exports = [
    {
        method: 'GET',
        path: '/product',
        handler: (request, h) => {
             //something is not working here
            Product.find(function(error, product) {
                if (error) {
                    console.error(error);
                }

                return h.view('product/product.html')
            })
        }
    }
]

Now I tried going to http://localhost:3000/product but when I go there, it is saying :

{
"statusCode": 500,
"error": "Internal Server Error",
"message": "An internal server error occurred"
}

and inside the terminal is this Debug: internal, implementation, error Error: handler method did not return a value, a promise, or throw an error

What I did:

I go to my terminal and type mongo and show dbs , I noticed that hapidb database is not in here so I thought maybe that is the problem. So I went and typed use hapidb and db.products.insert({name: "Television"})

I tried restarting node app.js and going to http://localhost:3000/product . Still not working.

Now I tried changing my route handler slightly to this

 handler: (request, h) => {
                //I added an empty object to find all product
        Product.find({}, function(error, product) {
            if (error) {
                console.error(error);
            }

            return h.view('product/product.html')
        })
    }

This is all I can think of. If I only return return h.view('product/product.html') It is working. but with Product.find in the code, It is not working.

Please help

Upvotes: 1

Views: 345

Answers (1)

Cedomir Rackov
Cedomir Rackov

Reputation: 1092

Try returning the Product.find like so:

handler: (request, h) => {
    return Product.find({}, function(error, product) { // <-- This line
        if (error) {
            console.error(error);
        }

        return h.view('product/product.html')
    })
}

Upvotes: 3

Related Questions