Please note that I posted Part 1 previously. I am running windows 10 and use vscode. At the end I show my package.json showing the versions of all packages used. Here are the issues I ran into and solutions found in the second half of this great course.
Recap on Joi.Validate() - does NOT work since version 14.
Instead of joi.validate(), use schema.validate(). For genre.js use the following. Similarly for customer, movie, rental and user models.
function validateGenre(genre) {
const schema = Joi.object({
name: Joi.string().min(5).max(50).required(),
});
const result = schema.validate(genre);
return result;
}
For movies.js use âŚ
function validateMovie(movie) {
const schema = Joi.object({
title: Joi.string().min(5).max(50).required(),
genreId: Joi.string().required(),
numberInStock: Joi.number().min(0).required(),
dailyRentalRate: Joi.number().min(0).required(),
});
return schema.validate(movie);
}
Section 8. Transactions - Fawn issues (using âfawnâ: â^2.1.5â)
Had lots of errors including âcould not find a declaration fileâ and @types/fawn@ is not in this registry!
Solution: in routes rental.js instead of Fawn.init(âmongooseâ); use :
Fawn.init("mongodb://127.0.0.1:27017/vidly");
Rentals Validate FAILS (see Joi issue above). Use this code instead âŚ
function validateRental(rental) {
const schema = Joi.object({
customerId: Joi.string().required(),
movieId: Joi.string().required(),
});
return schema.validate(rental);
}
Logging Errors
winston.handleExceptions(new winston.transports.File({filename: "uncaughtExceptions.log"}))
Above is deprecated and instead use below.
winston.exceptions.handle(
new winston.transports.File({ filename: "uncaughtExceptions.log" })
);
Hyper Terminal on Windows - you CAN use export to set your environment variable eg
export vidly_jwtPrivateKey=mySecureKey
Test-Driven Development
await.Rental.Remove({}) // deprecated, replace remove with deleteone, deleteMany eg
await Rental.deleteMany({});
Another issue I had was an error âListen EADDRINUSE: address already in use:::3000â when running the integration tests. If I ran just genres.text.js (renamed others to auth.xxxx.js and returns.xxxx.js so they wouldnât be recognised as tests) then all tests passed. Similarly if I just ran auth or returns, all tests ran fine, but as soon as I run two files I get that error and if I run all three then I get that error twice. So there is an issue with running more than one file of tests!
A solution I saw on here, which worked for me (ie all three files of tests run and all tests pass and no errors) is to add âmaxWorkers=1 to end of scripts line in package.json. (I saw another option ârunInBand that could also work).
"test": "jest --watchAll --verbose --maxWorkers=1"
Code Review - Checked final course code with mine.
In final code provided, hash.js and home.js were excluded but should not have been.
Models - all the validate functions I changed as noted above because I am using âjoiâ: â^17.6.0â (on Windows). See note above on Joi.
Logging.js - final code had require(âwinston-mongodbâ) commented out. I used this without error (âwinston-mongodbâ: â^5.0.7â )
Test code - ".Remove({}) deprecated and should be replace with .deleteMany({}) which works fine.
Thats it! Well, I still have to deploy to git and heroku but I want to post something separate on that as I have deployed a few apps now and hit some other issues on that which I will post here shortly.
Package.json
Below is my package.json to show what package versions I am using. I am running on Windows 10 pc.
{
"name": "vidly",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --watchAll --verbose --maxWorkers=1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.1",
"config": "^3.3.7",
"express": "^4.17.3",
"express-async-errors": "^3.1.1",
"fawn": "^2.1.5",
"joi": "^17.6.0",
"joi-objectid": "^4.0.2",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"moment": "^2.29.1",
"mongoose": "^6.2.4",
"winston": "^3.6.0",
"winston-mongodb": "^5.0.7"
},
"devDependencies": {
"jest": "^27.5.1",
"supertest": "^6.2.2"
}
}