Kokulan
Kokulan

Reputation: 1364

Mongoose get value from embedded document

enter image description herei have a scheme like this

 var WFWorkItemDocument = new Schema({
        id: { type: String, required: true, unique: true, default: uuid.v1 },
        description: { type: String },
        period: [{
            id: { type: String, default: uuid.v1 },
            start: { type: Date, default: Date.now }
            due: { type: Number, integer: true },
            new: { type: Number, integer: true },
        }],

i want to get the period's due value for that i used a method like

    WorkItem.findOne({ id: idUpdate }, function(err, WorkItem) {
            if (err) {
                console.log("invlaid id");
                //return res.send(404, { error: 'invalid id' });
            }

            if (WorkItem) {
                console.log("id");
                console.log(WorkItem.period.due);

            } else {
                //res.send(404, new Error('Workitem not found'));
            }
});

but it doesn't work how can i get the due value??

This is the result for console.log(WorkItem)

Upvotes: 0

Views: 566

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

Change the schema to embed one object. Unless you need embedded array.

 var WFWorkItemDocument = new Schema({
        id: { type: String, required: true, unique: true, default: uuid.v1 },
        description: { type: String },
        period: {
            id: { type: String, default: uuid.v1 },
            start: { type: Date, default: Date.now }
            due: { type: Number, integer: true },
            new: { type: Number, integer: true },
        },

And if you define it as an embedded array, you can access like :

WorkItem.period[index].due

Upvotes: 2

Related Questions