Backend issue in react native course

TypeError: Joi.validate is not a function

The Joi API has changed since Mosh recorded the course and you may need to use schema.validate as shown here.

Hi Eelsholz,

I have tried to follow the reference you gave regarding the ‘Joi.validate not a function’ error in React Native backend project. Unfortunately, the error still persist after using schema.validate and changing the schema.

Please, can you share your validation.js and listings.js files. I’ll like to see what I’m doing wrong. I’ll appreciate your assistance.

Thanks
Yemi

You’re still getting Joi.validate is not a function even after removing Joi.validate? Weird!

I don’t have a validation.js and listings.js. Could you share yours?

Thanks for your response.
Here is the listings.js router:

const express = require(“express”);
const router = express.Router();
const Joi = require(“joi”);
const multer = require(“multer”);

const store = require(“…/store/listings”);
const categoriesStore = require(“…/store/categories”);
const validateWith = require(“…/middleware/validation”);
const auth = require(“…/middleware/auth”);
const imageResize = require(“…/middleware/imageResize”);
const delay = require(“…/middleware/delay”);
const listingMapper = require(“…/mappers/listings”);
const config = require(“config”);

const upload = multer({
dest: “uploads/”,
limits: { fieldSize: 25 * 1024 * 1024 },
});

const schema = Joi.object({
title: Joi.string().required(),
description: Joi.string().allow(“”),
price: Joi.number().required().min(1),
categoryId: Joi.number().required().min(1),
location: Joi.object({
latitude: Joi.number().required(),
longitude: Joi.number().required(),
}).optional(),
});

const validateCategoryId = (req, res, next) => {
if (!categoriesStore.getCategory(parseInt(req.body.categoryId)))
return res.status(400).send({ error: “Invalid categoryId.” });

next();
};

router.get(“/”, (req, res) => {
const listings = store.getListings();
const resources = listings.map(listingMapper);
res.send(resources);
});

router.post(
“/”,
[
// Order of these middleware matters.
// “upload” should come before other “validate” because we have to handle
// multi-part form data. Once the upload middleware from multer applied,
// request.body will be populated and we can validate it. This means
// if the request is invalid, we’ll end up with one or more image files
// stored in the uploads folder. We’ll need to clean up this folder
// using a separate process.
// auth,
upload.array(“images”, config.get(“maxImageCount”)),
validateWith(schema),
validateCategoryId,
imageResize,
],

async (req, res) => {
const listing = {
title: req.body.title,
price: parseFloat(req.body.price),
categoryId: parseInt(req.body.categoryId),
description: req.body.description,
};
listing.images = req.images.map((fileName) => ({ fileName: fileName }));
if (req.body.location) listing.location = JSON.parse(req.body.location);
if (req.user) listing.userId = req.user.userId;

store.addListing(listing);

res.status(201).send(listing);

}
);

module.exports = router;

And this is the validation.js module: I commented the initial code from Mosh

module.exports = schema => (req, res, next) => {
const result = schema.validate(req.body);

if (result.error)
return res.status(400).send({ error: result.error.details[0].message });

next();
};

// module.exports = schema => (req, res, next) => {
// const result = schema.validate(req.body);

// if (result.error)
// return res.status(400).send({ error: result.error.details[0].message });

// next();
// };

I’m trying some of your code and I’m not running into any errors yet. Do you have the exact error message, and the stack trace?