Me gustaria saber como introducir datos en un array de subdocumentos en mongodb.
Estoy trabajando en un proyecto de MEAN Stack con Angular 4.
Tengo el siguiente schema de mongoose:
var mongoose = require('mongoose');
var ChallengeSchema = new mongoose.Schema({
// _id: String,
name: String,
category: String,
subcategory: String,
level: Number,
topic: String,
questions: [{
questionText: String,
answer:[{
answerText: String,
succesful: Boolean
}]
}]
});
module.exports = mongoose.model('Challenge', ChallengeSchema);
Para leer datos desde la bbdd no tengo ningun problema, utilizando el siguiente codigo:
<dl class="list">
<dt>ID</dt>
<dd>{{ challenge._id }}</dd>
<dt>Category</dt>
<dd>{{ challenge.category }}</dd>
<dt>Subcategory</dt>
<dd>{{ challenge.subcategory }}</dd>
<dt>Topic</dt>
<dd>{{ challenge.topic }}</dd>
<dt>Level</dt>
<dd>{{ challenge.level }}</dd>
</dl>
<div class="container" *ngIf="challenge.questions">
<div class="row col-12" *ngFor="let question of challenge.questions">
<dl class="list col-12">
<dt>Question<dt>
<dd>{{ question.questionText }}</dd>
<div class="row">
<dl class="list col-3" *ngFor="let answer of question.answer">
<dt>Answer</dt>
<dd>{{ answer.answerText }}</dd>
<dt>Succesful</dt>
<dd>{{ answer.succesful }}</dd>
</dl>
</div>
</dl>
El problema lo tengo al intentar introducir datos en el array de subdocumentos tengo el siguiente html
<div class="container">
<h1>Add New Challenge</h1>
<div class="row">
<div class="col-md-6">
<form (ngSubmit)="saveChallenge()" #challengeForm="ngForm">
<div class="form-group">
<label for="name">Challenge Name</label>
<input type="text" class="form-control" id="name"
[(ngModel)]="challenge.name" name="name" required>
</div>
<div class="form-group">
<label for="category">Category</label>
<select class="form-control" [(ngModel)]="challenge.category"
name="category" required>
<option *ngFor="let c of categoryArray">{{ c }}</option>
</select>
</div>
<div class="form-group">
<label for="subcategory">Subcategory</label>
<input type="text" class="form-control" id="subcategory"
[(ngModel)]="challenge.subcategory" name="subcategory" required>
</div>
<div class="form-group">
<label for="topic">Topic</label>
<input type="text" class="form-control" id="topic"
[(ngModel)]="challenge.topic" name="topic" required>
</div>
<div class="form-group">
<label for="level">Level</label>
<select class="form-control" [(ngModel)]="challenge.level" name="level">
<option *ngFor="let l of levelArray">{{ l }}</option>
</select>
</div>
<div class="form-group" *ngFor="let q of questionsA" >
<label for="questionText">Question</label>
<input type="text" class="form-control" id="question"
[(ngModel)]="challenge.questions[q].questionText"
name="questions[q].questionText">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success"
[disabled]="!challengeForm.form.valid">Save</button>
</div>
</form>
</div>
</div>
</div>
Y este es el componente de Angular
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ChallengeService } from '../../services/challenge/challenge.service';
@Component({
selector: 'app-challenge-create',
templateUrl: './challenge-create.component.html',
styleUrls: ['./challenge-create.component.css']
})
export class ChallengeCreateComponent implements OnInit {
challenge = {};
question = {};
levelArray: Number [];
questionsA: Number [];
categoryArray: String [];
constructor(private challengeService: ChallengeService, private
router: Router) { }
ngOnInit() {
for (let x = 1 ; x <= 20; x++) {
if ( x === 1) {
this.levelArray = [x];
}else {
this.levelArray.push(x);
}
}
this.categoryArray = ['Core', 'Use', 'Build', 'Think'];
this.questionsA = [1];
}
saveChallenge() {
this.challengeService.saveChallenge(this.challenge).then((result) => {
const id = result['_id'];
this.router.navigate(['/challenge-details', id]);
}, (err) => {
console.log(err);
});
}
}
Quiero meter datos en el array de subdocumentos de questions para pobrar que funciona y luego supongo que sera igual para el de answer