# Node index.js error

**URL:** <https://forum.codewithmosh.com/t/node-index-js-error/14348>\
**Category:** React\
**Created:** [August 12, 2022, 5:29pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348 "2022-08-12T17:29:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlmTalal](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@AlmTalal](https://forum.codewithmosh.com/u/AlmTalal)\
**Post date:** [August 12, 2022, 5:29pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/1 "2022-08-12T17:29:54Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/b/b6ab96cadec58d872f6f734e53e4ab9d517c609b.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/3/30184fdc1658ffe14039833b8ebddcb0f86b854c.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/4/47f9bad39e78eac093193dc18345df31ee9516bf.png)

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 12, 2022, 5:38pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/2 "2022-08-12T17:38:21Z")

</div>

From [Consider removing all direct references to require("mongoose") in the code · Issue #48 · e-oj/Fawn · GitHub](https://github.com/e-oj/Fawn/issues/48), 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.

---

<div class="post-metadata">

**Author:** ![AlmTalal](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@AlmTalal](https://forum.codewithmosh.com/u/AlmTalal)\
**Post date:** [August 12, 2022, 5:46pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/3 "2022-08-12T17:46:45Z")

</div>

This is what I have on rentals.js

```auto
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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 12, 2022, 5:54pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/4 "2022-08-12T17:54:07Z")

</div>

> [@AlmTalal](#):
>
> ```auto
> Fawn.init(mongoose);
> 
> ```

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)](https://forum.codewithmosh.com/t/mongoose-transactions-vs-fawn-two-phase-commits/3281)

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.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 12, 2022, 9:55pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/5 "2022-08-12T21:55:42Z")

</div>

From [the Fawn documentation](https://npm.io/package/fawn), it looks like they expect you to do the following:

```auto
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.

---

<div class="post-metadata">

**Author:** ![AlmTalal](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@AlmTalal](https://forum.codewithmosh.com/u/AlmTalal)\
**Post date:** [August 13, 2022, 3:29pm UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/6 "2022-08-13T15:29:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dalare](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@Dalare](https://forum.codewithmosh.com/u/Dalare)\
**Post date:** [January 21, 2023, 3:55am UTC](https://forum.codewithmosh.com/t/node-index-js-error/14348/7 "2023-01-21T03:55:23Z")

</div>

Hi, I’m stuck in this course as well and I happen to be stumbling into one problem after another so I was wondering what approach did you take to understand this course? Mosh wont be releasing new React course anytime soon and I have already started to learn previous courses with latest version of everything but this course has been a big pain since most of the topics are outdated. Any help would be appreciated. Thanks!
