maregor
maregor

Reputation: 797

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?

Upvotes: 0

Views: 183

Answers (2)

yojna
yojna

Reputation: 513

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}}

Upvotes: 2

Vishnu
Vishnu

Reputation: 12283

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

Upvotes: 1

Related Questions