Node index.js error

Now I’m having this problem with the backend of the vidly project, the video is the 20 from calling backend services in the react js course, I had to upgrade my mongoose version due to a past error in the app. If anyone knows how to solve it please tell me.



From Consider removing all direct references to require("mongoose") in the code · Issue #48 · e-oj/Fawn · GitHub, it looks like that error is thrown if fawn was initialized improperly (eg. passing an empty mongoose instance). It appears to be referenced from the routes\rentals.js file so can you paste whatever you have in rentals.js? Maybe also whatever you have in startup\routes.js since that seems related as well.

This is what I have on rentals.js

const { Rental, validate } = require("../models/rental");
const { Movie } = require("../models/movie");
const { Customer } = require("../models/customer");
const auth = require("../middleware/auth");
const mongoose = require("mongoose");
const Fawn = require("fawn");
const express = require("express");
const router = express.Router();

Fawn.init(mongoose);

router.get("/", auth, async (req, res) => {
  const rentals = await Rental.find()
    .select("-__v")
    .sort("-dateOut");
  res.send(rentals);
});

router.post("/", auth, async (req, res) => {
  const { error } = validate(req.body);
  if (error) return res.status(400).send(error.details[0].message);

  const customer = await Customer.findById(req.body.customerId);
  if (!customer) return res.status(400).send("Invalid customer.");

  const movie = await Movie.findById(req.body.movieId);
  if (!movie) return res.status(400).send("Invalid movie.");

  if (movie.numberInStock === 0)
    return res.status(400).send("Movie not in stock.");

  let rental = new Rental({
    customer: {
      _id: customer._id,
      name: customer.name,
      phone: customer.phone
    },
    movie: {
      _id: movie._id,
      title: movie.title,
      dailyRentalRate: movie.dailyRentalRate
    }
  });

  try {
    new Fawn.Task()
      .save("rentals", rental)
      .update(
        "movies",
        { _id: movie._id },
        {
          $inc: { numberInStock: -1 }
        }
      )
      .run();

    res.send(rental);
  } catch (ex) {
    res.status(500).send("Something failed.");
  }
});

router.get("/:id", [auth], async (req, res) => {
  const rental = await Rental.findById(req.params.id).select("-__v");

  if (!rental)
    return res.status(404).send("The rental with the given ID was not found.");

  res.send(rental);
});

module.exports = router;

And this is on Routes.js

const express = require('express');
const genres = require('../routes/genres');
const customers = require('../routes/customers');
const movies = require('../routes/movies');
const rentals = require('../routes/rentals');
const users = require('../routes/users');
const auth = require('../routes/auth');
const returns = require('../routes/returns');
const error = require('../middleware/error');

module.exports = function(app) {
  app.use(express.json());
  app.use('/api/genres', genres);
  app.use('/api/customers', customers);
  app.use('/api/movies', movies);
  app.use('/api/rentals', rentals);
  app.use('/api/users', users);
  app.use('/api/auth', auth);
  app.use('/api/returns', returns);
  app.use(error);
}

So this is the line that is triggering the error. It appears to be having a problem with that instance of mongoose.

There is a whole thread about transactions using Fawn (which has not been updated in years) and some code for changing this to just skip Fawn entirely (preferring to use mongoose transactions directly): [Mongoose] Transactions vs Fawn (Two Phase Commits)

I think that the basic issue here is just that Fawn does not know how to recognize the current instance of mongoose since it has not been updated in so long.

From the Fawn documentation, it looks like they expect you to do the following:

var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017/testDB");

Fawn.init(mongoose);

Perhaps you have somewhere else in your codebase that you are calling mongoose.connect, but it is not happening early enough for Fawn? Otherwise I would recommend finding a way to stop using Fawn since it is so outdated.

I will try to learn a little about mongo, beacause I’m not understanding exactly what is the problem, but Thankss