Link to home
Start Free TrialLog in
Avatar of Gene Klamerus
Gene KlamerusFlag for United States of America

asked on

Getting errors trying to run a sample node/express example I copied from YouTube.

I've been building out a sample node/express example that Mosh put up on YouTube.

The issue at the moment is errors in using joi to validate a post request.

At this point the code is very small so I'm including it here.  It's just one file.

const Joi = require('joi');
const express = require('express');
const app = express();


app.use(express.json());


const courses = [
    {id: 1, name: 'course1'},
    {id: 2, name: 'course2'},
    {id: 3, name: 'course3'},
];




app.get('/', (req, res) => {
    res.send('Hello World!!!');
});


app.get('/api/courses', (req,res) => {
    // res.send([1, 2, 3]);
    res.send(courses);
});


app.get('/api/courses/:id', (req, res) => {
    //res.send(req.params.id);
    const course = courses.find(c => c.id === parseInt(req.params.id));
    if (!course)
        res.status(404).send('A course with the provided id was not found');
    res.send(course);
});




app.post('/api/courses', (req, res) => {


    const schema = {
        name:  Joi.string().min(3).required()
    };


    const result = Joi.validate(req.body, schema);


    if (result.error) {
        res.status(400).send(result.error);
        return;
    }


    const course = {
        id: courses.length + 1,
        name: req.body.name
    };
    courses.push(course);
    res.send(course);
});


const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));

Open in new window



The problem shows up when the code hits the Joi.validate step.  This generate the following error statement:

TypeError: Joi.validate is not a function

I'm afraid I'm completely baffled.  I'm trying to use a post from postman with the raw body content of
{
   "name": "Goo"
}

Open in new window


This should not generate any errors, but if the complaint is with the validate statement I think I've got an earlier syntax error or typo of some sort.



Avatar of Gene Klamerus
Gene Klamerus
Flag of United States of America image

ASKER

I should have added that I just downloaded joi today.  I have version 7.15.1
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very interesting.  I didn't know we had any better ways of documenting uploaded to EE.  Thanks for the tip.
Thanks for the direction.

I was just following what was done in the video (one from Mosh doing express/node.js, but it's from 2018 so I'm guessing that joi got a major rework and what he recorded no longer works.  I'll switch things around today and see how that goes.
Thanks a lot.  That got me in the right direction.  I made the couple of changes below and things are fine now.

    const schema = Joi.object({
        name:  Joi.string().min(3).required(),
    });

    const result = schema.validate(req.body);

Open in new window

You are welcome.