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 in return 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?