skip to Main Content

I have written the same code as the document suggests.

const mongoose = require("mongoose");
const Fawn = require("fawn");

mongoose
  .connect("mongodb://0.0.0.0:27017/rental")
  .then(() => console.log("Database rental connected"))
  .catch((err) => console.log(err.message));

Fawn.init(mongoose);

And the error is:

C:xampphtdocsmongo-rentalnode_modulesfawnlibfawn.js:30
        throw new Error("The provided mongoose instance is invalid");
        ^

Error: The provided mongoose instance is invalid
    at Object.init (C:xampphtdocsmongo-rentalnode_modulesfawnlibfawn.js:30:15)
    at Object.<anonymous> (C:xampphtdocsmongo-rentalindex.js:15:6)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

I have followed the document and still getting the same error.
I have already removed and reinstalled both the library two times.

3

Answers


  1. I have resolved this by changing obj.modelScemas to obj.Schema in isMongoose(obj) function.

    Fawn version = 2.1.5

    Steps to make changes:

    1. goto "node_modules/fawn/lib/fawn.js"
    2. Scroll down until you see "function isMongoose(obj) { … }".
    3. change "obj.modelScemas" to "obj.Schema" -> save
    4. Come back to project’s root folder and run the index file -> node index.js

    Note: this is not a recommended solution. It is just a work around until the issue gets fixed in newer version.

    Login or Signup to reply.
  2. I resolved this issue with this documentation enter link description here

    Login or Signup to reply.
  3. I believe that there has been no update of FAWN since 6 years if you check the documentation in Github.
    instead of FAWN you can use transaction since MongoDb and Mongoose support transactions.
    this is how I resolve it myself

    const db = await mongoose.createConnection('mongodb://localhost/playground').asPromise();
    const session = await db.startSession();
    
    try {
        await session.withTransaction(async () => {
          rental.save();
          decNumberInStock(movie._id)
          res.send(rental);
        });
      } finally {
        await session.endSession();
        await db.close();
        console.log('ok')
      }
    

    in movies model you can define decNumberInstock :

    async function decNumberInStock(movieId) {
    const movie = await Movie.findByIdAndUpdate(movieId , {
        $inc : { numberInStock : -1}
    });
    movie.save();}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search