e-oj / Fawn

Transactions for MongoDB (See the README)
https://www.npmjs.com/package/fawn
MIT License
486 stars 54 forks source link

Schema hasn't been registered for model #71

Open skarif2 opened 6 years ago

skarif2 commented 6 years ago

Fawn: 2.1.5 MongoDB: 3.6

Error: Schema hasn't been registered for model \"Promotion\".\nUse mongoose.model(name, schema)

My Code that is working:

  promotion.save()
    .then((savedPromotion) => {
      const merchant = req.merchant;
      merchant.promotion = savedPromotion._id;
      return merchant.save();
    })
    .then(() => {
      res.json({
        status: 'success',
        message: 'Promotion successfully added.'
      });
    })
    .catch((err) => {
      res.json({
        status: 'error',
        message: err.message
      });
    });

Using Fawn which is not working:

  const Task = Fawn.Task();
  Task.save(Promotion, promotion)
    .update(Merchant, { _id: req.merchant._id }, {
      promotion: { $ojFuture: '0._id' }
    })
    .run({ useMongoose: true })
    .then(() => {
      res.json({
        status: 'success',
        message: 'Merchant promotion successfully added.'
      });
    })
    .catch((err) => {
      res.json({
        status: 'error',
        message: err.message
      });
    });

Both the model has reference to each other. I know circular reference is bad but couldn't find any other easier way. And I am being extra cautious while populating references.

So, am I missing something? Is there any workaround?

e-oj commented 6 years ago

Try

Task.save(promotion)

Instead of

Task.save(Promotion, promotion)
skarif2 commented 6 years ago

Tried it. Getting the same error.

e-oj commented 6 years ago

Can you put up the code for the Promotion model?

skarif2 commented 6 years ago

Promotion model: promotion.model.txt

BTW if I run bellow code and try to update only Merchant I get this error:

Error: Schema hasn't been registered for model \"Merchant\".\nUse mongoose.model(name, schema)

const Task = Fawn.Task();
Task
  // .save(Promotion, promotion)
  .update(Merchant, { _id: req.merchant._id }, {
    // promotion: { $ojFuture: '0._id' }
  })
  .run({ useMongoose: true })
  .then(() => {
    res.json({
      status: 'success',
      message: 'Merchant promotion successfully added.'
    });
  })
  .catch((err) => {
    res.json({
      status: 'error',
      message: err.message
    });
  });
e-oj commented 6 years ago

What version of Fawn are you using?

skarif2 commented 6 years ago

Fawn: 2.1.5 MongoDB: 3.6

e-oj commented 5 years ago

It might be tied to the mongoose version #74