Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broke descriminator model instance toObject() logic #20

Open
leviskay opened this issue Aug 18, 2020 · 0 comments
Open

Broke descriminator model instance toObject() logic #20

leviskay opened this issue Aug 18, 2020 · 0 comments

Comments

@leviskay
Copy link

Use this plugin broke standart descriminator modle instance toObject logic, discriminator model fields don't fall in object, but accessible by dot.

const assert = require("assert");
const mongoose = require("mongoose");
const mongoRestActions = require("mongoose-rest-actions");

mongoose.plugin(mongoRestActions); // try to comment or not

const GITHUB_ISSUE = `gh6701`;
const connectionString = `mongodb://localhost:27017/${GITHUB_ISSUE}`;

run()
  .then(() => console.log("done"))
  .catch((error) => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });
  await mongoose.connection.dropDatabase();

  const membershipSchema = mongoose.Schema(
    {
      name: { type: String, required: true },
      price: { type: Number, required: true },
    },
    { discriminatorKey: "type" }
  );

  const subscriptionSchema = mongoose.Schema({
    frequency: {
      type: String,
      required: true,
    },
  });

  const Membership = mongoose.model("Membership", membershipSchema);
  const Subscription = Membership.discriminator(
    "Subscription",
    subscriptionSchema
  );

  await Subscription.create({
    name: "Monthly subscription",
    price: 1,
    frequency: "month",
  });

  const sub = await Membership.findOne({ price: 1 });

  console.log(sub); // don't have frequency field
  assert.equal(sub.frequency, "month"); // that's ok
}

Because of this it is not possible to send model-dependent field descriptor by express res.json() method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant