SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.24k stars 664 forks source link

this.ownerDocument(...).model is not a function when attempting to add new instance of subdocument #181

Closed William-Hill closed 2 years ago

William-Hill commented 5 years ago

I have a schema called Contact and another Schema called email that is a subdocument of Contact. The Contact schema can hold an array of emails.

When attempting to add a new instance of email, I get the following error in the AdminBro UI: this.ownerDocument(...).model is not a function.

The error shows up on the ID field of the email subdocument

How can this be fixed?

wojtek-krysiak commented 5 years ago

can you paste that schemas and admin-bro config?

William-Hill commented 5 years ago

The admin-bro config is as follows

const AdminBroOptions = { resources: ['Contact', 'Email', 'Phone', 'URL'], rootPath: "/admin", };

The schemas are below:

 export const emailSchema: mongoose.Schema = new mongoose.Schema({
    order: { type: Number, default: 0 },
    email: { type: String, match: [/\S+@\S+\.\S+/, "is invalid"], maxlength: 250 },
    timestamp: { type: Date, default: Date.now },
}, { collection: "contact_email", _id: false });
const contactSchema: mongoose.Schema = new mongoose.Schema({
   name: { type: String, required: true, match: [/^[a-zA-Z0-9]+$/, "is invalid"], maxlength: 250 },
    jobTitle: { type: String, maxlength: 250 },
    organization: { type: String},
    description: { type: String},
    address: { type: String },
    category: { type: mongoose.Schema.Types.ObjectId, ref: "ContactCategory" },
    group: { type: mongoose.Schema.Types.ObjectId, ref: "ContactGroup" },
    order: { type: Number, default: 0},
    email: { type: [emailSchema], default: [] },
    phone: {type: [phoneSchema], default: [] },
    fax: { type: [faxSchema], default: [] },
    url: { type: [urlSchema], default: [] },
    timestamp: { type: Date, default: Date.now }
}, {collection: "contact"});

The phone and fax schemas are similar to the email schema.

The category and group are working fine as foreign keys, but I'm only having problems with the subdocuments

wojtek-krysiak commented 5 years ago

sorry for the late response - will try to debug that this week and let you know

germanliu commented 4 years ago

Hi all. Was this resolved? I have the same issue, any help is welcome. Thanks in advance!

wojtek-krysiak commented 4 years ago

we are making lots of improvements/bug fixes regarding nested arrays/schemas. So this should probably go away in v3.3 (currently in beta)

dziraf commented 2 years ago

3.3 was released a long time ago so this should've been fixed. Please open another issue if you're still having problem with your case.