Node: Problems & Solutions - Part 2 of 3 (course completed, except deployment - see Part 3)

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"
  }
}

Hey Kavfam,

try this validation,

function validateGenre(genre) {
const schema = Joi.object({
name: Joi.string().min(3).required(),
});
return schema.validate(genre, {
abortEarly: false,
});
}

function validateRental(rental) {
const schema = Joi.object({
customerId: Joi.string().required(),
movieId: Joi.string().required(),
});

return schema.validate(rental, {
abortEarly: false,
});
}

function validateMovie(movie) {
const schema = Joi.object({
title: Joi.string().min(3).max(50).required(),
genreId: Joi.string().required(),
numberInStock: Joi.number().min(0).max(50).required(),
dailyRentalRate: Joi.number().min(0).max(50).required(),
});

return schema.validate(movie, {
abortEarly: false,
});
}

My collections in mongo compass are all empty

This is probably because at some point you launched mongod in a different context.

I had 3 different versions when I hit this issue:

  • default location: C:\data
  • C:\Program Files\MongoDB\Server\5.0\data
  • C:\Program Files\MongoDB\Server\6.0\data

Copy the folders and files in C:\data\ to one side.
Then copy the contents of the other locations you have (e.g. above) into C:\data
Rerun MongoDB Compass in between.
You should find your Collections at some point this way.