mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Nested Array structured not being returned as defined

Open Enado95 opened this issue 7 years ago • 2 comments

I have an express API using an already populated mongoDB and have defined the schema like so:

const accountHolderSchema= new mongoose.Schema({
  pid: {Type: Number},
  accountNumber: {type: String},
  relationshipType: {type: String},
  firstName: {type: String},
  middleName: {type: String},
  lastName: {type: String}
});

const accountsSchema = new mongoose.Schema({
  accountNumber: String,
  accountType: String,
  accountHolder: [accountHolderSchema]
});

 
const productDetailSchema = new mongoose.Schema({
  pid: Number,
  accounts: [accountsSchema]
});

I have literally copied and paste all the properties and from the database so i know they match so i know that's out of the picture

The RESPONSE I get is this:

{
"pid": 2697143,
    "accounts": [
        {
            "accountHolders": [
                {
                    "pid": 13209741,
                    "accountNumber": "403716000062",
                    "relationshipType": "BENEFICIARY",
                    "firstName": "Maria",
                    "middleName": "Delores",
                    "lastName": "Jackson"
                }
             ]
            "accountNumber": "12345",
            "accountType": "RSA",
        }
     ]
}

BUT what the response I WANT to get is this:

{
"pid": 2697143,
    "accounts": [
        {
            "accountNumber": "12345",
            "accountType": "RSA",
            "accountHolders": [
                {
                    "pid": 13209741,
                    "accountNumber": "403716000062",
                    "relationshipType": "BENEFICIARY",
                    "firstName": "Maria",
                    "middleName": "Delores",
                    "lastName": "Jackson"
                }
             ]
        }
     ]
}

I want the accountNumber and accountNumber to come before accountHolders field.

I'm not sure if its the way how i define a nested array inside of another nested array that's throwing of the structure. If I don't the define accountHolderSchema the structure is returned fine. Any ideas?

Enado95 avatar Apr 09 '19 15:04 Enado95

Hey @Enado95, it looks like you have an issue open for this already, with pretty much identical content: https://github.com/Automattic/mongoose/issues/7692

It's not generally a good idea to post duplicates. I'm not a formal contributor on this project, so I can't close this issue -- but I imagine someone like @vkarpov15 would close this and ask to keep any updates on the original issue.

wcarss avatar Apr 11 '19 16:04 wcarss

This one can be closed

Enado95 avatar Apr 11 '19 17:04 Enado95