How do I override/create a mongoose Model function?

Viewed 55

I was trying to create a new function for a Model not a instance. In other words, a User.func() not a user.func().

I read its doc and found statics keyword for model, and method keyword for instance. Here is what I was trying.

Transaction.statics.createTransactions = async (userOId, type, netChange) => {
  const oldBalance = await User.findOne({ _id: userOId }).points;
  return this.create({ userOId, type, balance: { netChange, newBalance: oldBalance + netChange } });
};

However, it reports this.create is not a function.

1 Answers

It turned out that I've forgotten that arrow function blinds 'this', sry for the bothering.

Related