0

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?

8
  • have you tried {{ccard.type}} in your template? Commented Aug 27, 2015 at 9:48
  • Yes, I've tried all of ccard's member variables, but none of the data in the JSON that corresponds to those variables appear when I run the site. Commented Aug 27, 2015 at 9:50
  • 1
    Can you add the HTML template code to the question. Where is digits field in ProjectSchema? Commented Aug 27, 2015 at 9:50
  • @Mahesh my bad, that is number but I wrote it as digits. I will correct it. I will add the HTML template code. Commented Aug 27, 2015 at 9:55
  • If you are using windows (I don´t know if there is a linux version) you can use MongoVue to see if data is saved (it is similar at phpmyadmin... more or less). Commented Aug 27, 2015 at 10:12

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Trisha I don't think schema is correct ccard type is set as string.
@Mahesh all I did was format the code of the original answer - maybe yojnajaininfo can shed some more light on the answer?
1
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.