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

Support prop: [schema1, schema2] as a way of defining an array of length 2 with subdoc 0 having schema = schema1 and subdoc 1 having schema = schema2 #9972

Open
enkicoma opened this issue Feb 27, 2021 · 1 comment
Labels
new feature This change adds new functionality, like a new method or class

Comments

@enkicoma
Copy link

enkicoma commented Feb 27, 2021

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
The schema will create 2 objects as scripted, but it will iterate twice with the values defined for object 1 in a children array.
If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');

const subAddressAV = new mongoose.Schema(
{
    nameAV: {
        type: String,
        default: "Available"
    },
    colorAV: {
        type: String,
        default: "rgb(40%, 100%, 40%)"
    },
    available: {
        type: Number,
        // required: [true, 'Please Pick a Number'],
        default : 0,
        min: 0,
        max: 100
    }
}
);
const subAddressNV = new mongoose.Schema(
    {
        nameNV: {
            type: String,
            default: "NonAvailable"
        },
        colorNV: {
            type: String,
            default: "rgb(100%, 83.5%, 0%)"
        },
        nonavailable: {
            type: Number,
            // required: [true, 'Please Pick a Number'],        
            default : 0,
            min: 0,
            max: 100
        }
    }
);

const subAddress = new mongoose.Schema({
    name: {
        type: String,
        // required: [true, 'Please add a Name'],
        unique: true,
        maxLength: [20, 'Name cannot be more than 20 characters']
    },
    description: {
        type: String,
        // required: true,
        maxLength: [400, 'Description cannot be more than 400 characters']
    },
    color: {
        type: String,
        // required: true,
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    children: [subAddressAV, subAddressNV]
});

const NoteSchema = new mongoose.Schema({
    team_title: {
        type: String,
        default: "TEAM"
    },
    team_description: {
        type: String,
        default: "We!!"
    },
    team_color: {
        type: String,
        default: "rgb(50.2%, 0%, 50.2%)"
    },
    children: [subAddress]   
})

module.exports = mongoose.models.Note || mongoose.model('Note', NoteSchema);

N/A
What is the expected behaviour?
The behaviour: it should return and assign the values for both objects when doing a POST request
the Post request:

{
    "children": [
        {
            "name": "swwss",
            "description": "sswws s",
            "children": [
                {
                    "nameAV": "sswwws",
                    "colorAV": "rbg(ww)",
                    "available":10
                },
                {
                    "nameNV": "sss",
                    "colorNV": "rbg(sswwss)",
                    "nonavailable":11
                }
            ]
        }
    ]
}

and te return is totally wrong:

{
    "success": true,
    "data": {
        "team_title": "TEAM",
        "team_description": "We!!",
        "team_color": "rgb(50.2%, 0%, 50.2%)",
        "_id": "603a78cbea92dc2cd76cf198",
        "children": [
            {
                "_id": "603a78cbea92dc2cd76cf199",
                "name": "swwss",
                "description": "sswws s",
                "children": [
                    {
                        "nameAV": "sswwws",
                        "colorAV": "rbg(ww)",
                        "available": 10,
                        "_id": "603a78cbea92dc2cd76cf19a"
                    },
                    {
                        "nameAV": "Available",
                        "colorAV": "rgb(40%, 100%, 40%)",
                        "available": 0,
                        "_id": "603a78cbea92dc2cd76cf19b"
                    }
                ],
                "createdAt": "2021-02-27T16:52:27.747Z"
            }
        ],
        "__v": 0
    }
}

more information: https://stackoverflow.com/questions/66306598/next-js-mongoose-schema-wrong-children-values-at-a-post-request
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
I am using studio3t (localhost - mongo)

  "dependencies": {
    "d3": "5.12.0",
    "isomorphic-unfetch": "^3.1.0",
    "mongoose": "^5.11.17",
    "next": "10.0.6",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^2.0.3",
    "sunburst-chart": "1.11.2"
  }

and

node --version
v15.8.0

is this possible at all, to have 2 objects in a child schema array?

@vkarpov15
Copy link
Collaborator

Unfortunately, new Schema({ children: [subAddressAV, subAddressNV] }) is equivalent to new Schema({ children: [subAddressAV] }) in Mongoose. We will need to add a new feature to support this.

As a workaround, you'll need to use embedded discriminators, or structure your data as an object rather than an array:

new Schema({
  subAddressAV,
  subAddressNV
});

@vkarpov15 vkarpov15 changed the title Schema POST request returns wrong data. Support prop: [schema1, schema2] as a way of defining an array of length 2 with subdoc 0 having schema = schema1 and subdoc 1 having schema = schema2 Mar 1, 2021
@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Mar 1, 2021
@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Mar 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

2 participants