0

Original question: how to insert type as a field name.

So I'm getting the error:

CastError: Cast to embedded failed for value "{ details:\n   [ { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b13741b\',\n       title: \'Name\',\n       order: 0 },\n     { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b13741a\',\n       title: \'Price\',\n       order: 1 },\n     { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b137419\',\n       title: \'Company\',\n       order: 2 } ] }" at path "items"

here's the object:

[
    {
        "title": "Name",
        "content": "",
        "hidden": false,
        "order": 0,
        "type": "text"
    },
    {
        "title": "Price",
        "content": "",
        "hidden": false,
        "order": 1,
        "type": "text"
    },
    {
        "title": "Company",
        "content": "",
        "hidden": false,
        "order": 2,
        "type": "text"
    }
]

I have the query:

ListModel
    .findOneAndUpdate(
        {
            _id: list_id
        },
        {   
            $push: {
                items: {
                    details: the_template
                }
            }
        }
    )

which is trying to make an insertion/update

I'm not sure what the error is.

Noting that I am inserting a new item into the items array. It works if I create the item first, then update that item with a $push: {details} query.

Here are the schemas.

List Schema:

var List = new Schema(
    {
        items: {
            index: true,
            type: [Item]
        },
        hidden: {
            default: false,
            index: true,
            type: Boolean
        },
        name: {
            index: true,
            type: String,
        },
        item_details_template: {
            default: [
                {
                    "title": "Name",
                    "content": "",
                    "hidden": false,
                    "order": 0,
                    "type": "text"
                },
                {
                    "title": "Price",
                    "content": "",
                    "hidden": false,
                    "order": 1,
                    "type": "text"
                },
                {
                    "title": "Company",
                    "content": "",
                    "hidden": false,
                    "order": 2,
                    "type": "text"
                }
            ],
            type: [Item_Detail]
        },
        // Other users who have access
        shared_with: {
            index: true,
            refs: "users",
            type: [Shared_With]
        },
        // Owner
        user_id: {
            index: true,
            ref: "users",
            required: true,
            type: ObjectId,
        }
    },
    {
        strict: false
    }
)

Item Schema:

var Item = new Schema(
    {
        // Template_Item
        based_on: {
            index: true,
            type: ObjectId
        },
        details: {
            default: [],
            index: true,
            type: [Item_Detail],
        },
        display_name: {
            default: "",
            index: true,
            type: String,
        },
        image: {
            default: "http://via.placeholder.com/700x500/ffffff/000000/?text=No%20Image&",
            index: true,
            type: String
        },
        hidden: {
            default: true,
            index: true,
            type: Boolean
        },
        tags: {
            default: [],
            index: true,
            type: [Tag]
        }
    },
    {
        strict: false
    }
)

ItemDetail Schema:

var Item_Detail = new Schema(
    {
        content: {
            default: "",
            index: true,
            type: String,
            trim: true,
            validate: {
                validator(v)
                {
                    if (this.title.trim() === "price")
                    {
                        return !isNaN(v.trim())
                    }
                    return true
                }
            }
        },
        hidden: {
            default: false,
            index: true,
            type: Boolean
        },
        order: {
            index: true,
            required: true,
            type: Number
        },
        table: {
            default: {},
            type: Mixed
        },
        title: {
            required: true,
            index: true,
            type: String,
        },
        type: {
            default: "text",
            enum: ["text", "table"],
            index: true,
            type: String
        },
    },
    {
        strict: false
    }
)
10

1 Answer 1

0

The reason it was giving this error was due to the definition of variables. Item was being declared before Item_Detail, which means it had no access to Item_Detail at the time.

I.e.

var Item = new Schema(...) // Item_Detail is referenced in here
var Item_Detail = new Schema(...)

fixed with

var Item_Detail = new Schema(...) // declare before
var Item = new Schema(...)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.