简体   繁体   中英

Creating nested schema in mongoose

Suppose I have this Mongoose Schema in my models.js file:

var Mongoose = require('mongoose');

var ProjectSchema = new Mongoose.Schema({
    "name": String,
    "id": String,
    "phone": String,
    "address": String,
    "dob": String,
    "action": String, 
    "ccard": { 
        "type": String,
        "number": String,
        "status": String,
        "expiry": String
    }
});

exports.Project = Mongoose.model('Project', ProjectSchema);

And suppose I already have website that uses MongoDB to load data from a JSON file with the necessary information. How would I call ccard 's fields in a HTML template? As of now I'm able to call {{name}} and {{id}} in the {{each projects}} ... {{/each}} clause without issues, but when I call {{ccard.number}} it would not output anything.

HTML sample:

{{each projects}}
<table>
    <tr>
        <td>{{ccard.type}}</td>
        <td>{{ccard.number}}</td>
        <td>{{ccard.status}}</td>
        <td>{{ccard.expiry}}</td>                                                  
    </tr>
</table>
{{/each}}

Is the problem in the schema or the template variables or somewhere else?

Schema is written correctly. I can see

"ccard": { 
      "type": String,
      "number": String,
      "status": String,
      "expiry": String
}

you can access by your variable name in which document is saved. Let assume you fetched 1 document in

var = project

now you can access #{project.ccard.status} or {{project.ccard.status}}

var Mongoose = require('mongoose');

var ProjectSchema = new Mongoose.Schema({
    "name": String,
    "id": String,
    "phone": String,
    "address": String,
    "dob": String,
    "action": String, 
    "ccard": { 
        "type": {
            "type": String,
        }
        "number": String,
        "status": String,
        "expiry": String
    }
});

exports.Project = Mongoose.model('Project', ProjectSchema);

I am not sure whether above code works. If it doesn't change the column name as mentioned below.

var Mongoose = require('mongoose');

var ProjectSchema = new Mongoose.Schema({
    "name": String,
    "id": String,
    "phone": String,
    "address": String,
    "dob": String,
    "action": String, 
    "ccard": { 
        "ccartType": String,
        "number": String,
        "status": String,
        "expiry": String
    }
});

exports.Project = Mongoose.model('Project', ProjectSchema);

Your template code

{{each projects}}
<table>
    <tr>
        <td>{{ccard.ccartType}}</td>
        <td>{{ccard.number}}</td>
        <td>{{ccard.status}}</td>
        <td>{{ccard.expiry}}</td>                                                  
    </tr>
</table>
{{/each}}

Change the model as mentioned above and make sure the model is changed in the mongodb schema.

Reason might be it is taking ccard as a single column with type : string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM