I'm have a Referral
model defined and I'm trying to create an instance of this model. However when I do so, the request hangs indefinitely. When I console.log(Referral)
the object shows in console fine.
Here's my code:
// TODO: this hangs?
console.log("creating referral...");
const referral = await Referral.create({
referrer: referrer.email,
referee: user.email,
amount: 1000,
});
console.log("referral created");
Notice that referral created
is never actually printed to the console!
Here's the model:
const mongoose = require("mongoose");
const crypto = require("crypto");
const schema = new mongoose.Schema(
{
referrer: {
type: mongoose.Types.ObjectId,
required: true,
},
referee: {
type: mongoose.Types.ObjectId,
required: true,
},
// Number of cents, 1000 = $10
amount: {
type: Number,
required: true,
min: 0,
},
},
{ timestamps: true }
);
// A user can't refer the same user twice
schema.index({ referrer: 1, referee: 1 }, { unique: true });
module.exports = mongoose.model("Referral", schema);
After removing the schema.index
line, there was no change. I've also tried checking if I'm using connectionOpen
, with mongoose, but I'm not.
It turns out the problem was that I migrated the Referral
model to use the type of mongoose.Types.ObjectId
rather than string
. But I kept trying to pass referrer.email
as the argument for those fields.
After changing my code like so, it no longer hangs.
const referral = await Referral.create({
referrer: referrer,
referee: user,
amount: 1000,
});