Message from Hell: "Uncaught TypeError: Cannot read property 'insertDoc' of null"

I’m using a large nested schema, with children schemas within other schemas. Occasionally I need to access a value from a parent schema to use in an autoValue or autoform, to make things more readable to our users. I’m using the exact same schema tree for both insert and updates, with minimal controls for autoform in the template. specialTextA and specialTextV are entries from the parent schema. The parent schema includes an element of the type:[childSchema] . Note: I initially coded this with || statements (short circuit evaluation on the if (typeof) statements and the var = . Converted that to longhand to see if I could beat the error. Same error occurs, sigh.

Sample Code, from the child schema

childSchemaText: {
        type: String,
        optional: true,
        autoValue: function() {
            var valueA = "";
            var valueB = "";
            // the next line generates an Uncaught TypeError: Cannot read property 'insertDoc' of null
            if (typeof AutoForm.getFormValues().insertDoc.specialTextA !== 'undefined') {   <-- console error points here
                valueA= AutoForm.getFormValues().insertDoc.specialTextA;
            }
            if (typeof AutoForm.getFormValues().updateDoc.specialTextA !== 'undefined') {
                valueA = AutoForm.getFormValues().updateDoc.specialTextA;
            }
            if (typeof AutoForm.getFormValues().insertDoc.specialTextB !== 'undefined') {
                valueB = AutoForm.getFormValues().insertDoc.specialTextB;
            }
            if (typeof AutoForm.getFormValues().updateDoc.specialTextB !== 'undefined') {
                valueB = AutoForm.getFormValues().updateDoc.specialTextB;
            }
            console.log("interim content from within childSchema.childSchemaText: ", valueA, valueB);
            return "Reminder: check out " + valueA +", and also " + valueB;
        },
    },

Chrome browser console, after I entered Hamburger Shop and Tonys Pizza manually on the autoform.

interim content from within childSchema.childSchemaText:  Hamburger Shop Tonys Pizza
Uncaught TypeError: Cannot read property 'insertDoc' of null

That last console error refers to the file that holds the child schema and the line number points to the first occurance of if(typeof AutoForm.... ) as shown above…

What’s happening here? Why is this working (per the console output) but not working (as I’m seeing an error message, and no “Reminder” message pasted into the desired output field on the form)?

It’s been a long time since I used autoform, but doesn’t AutoForm.getFormValues() take the formId as a required param?

Yes, but. How does that work for an insert? I was looking for a this formId as a default, and that’s definitely how it seems to work. I’ve been using the AutoForm.getFormValues() all day in other places in my code, but elsewhere was within a autoform function. this is the only place I’m using it in a autovalue function.

You may well be right, but how do you get a formId for the new form you are generating right now? Hmm. as I look at the console output, perhaps the code is running once with the assumed formId of the parent, and then a second time with the formId of the child (which won’t have any data).

Again, haven’t used these packages in a bit, but is referencing the field with this.field('specialTextA') not an option?

1 Like

I could have sworn I tried that before. this.field(’…’) works great, even though the item is listed in a parent schema. I’m still unsure of how to get to the values for inserting new content. I know how to do an update for an existing collection item, not sure how to do an autovalue for items as they are being entered into a new form (with multitiered / nested schemas) Many thanks.

I’m not sure what all your use cases may be, but you can also use collection hooks to generate new fields and values on an insert (based on the doc being inserted).