troubleshooting Question

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

Avatar of Gene Klamerus
Gene KlamerusFlag for United States of America asked on
Node.js* Express* joiJavaScript
6 Comments1 Solution11 ViewsLast Modified:
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.



ASKER CERTIFIED SOLUTION
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 6 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 6 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004