How to add a javascript object in a dynamically received select option?

Asked

Viewed 61 times

0

Which is the best way to add information in the value of being that I receive an Object dynamically.

The current code is as follows (Note the _options[i] | json):

<ion-item *ngIf="_type.indexOf('select') > -1 || _type.indexOf('checkbox') > -1 || _type.indexOf('radiobutton') > -1">
    <ion-label floating>{{_title}}</ion-label>
    <ion-select title={{_type}} [multiple]="_type === 'select' ? false : null || _type === 'checkbox' ? true : null || _type === 'radiobutton' ? false : null"
        [(ngModel)]="answer" (ionChange)="inputPopulate($event, _id, answer)">
        <ion-option value="{{_options[i] | json}}" *ngFor="let option of _options; let i = index">{{_options[i].answer}}</ion-option>
    </ion-select>
</ion-item>

_options is something like: Opções do multselect

Screen code responsible for adding the selected option to this. _obj

<ion-slide *ngFor="let item of slide.sections">
            <h2 class="slide-title" [innerHTML]="item.name"></h2>
            <div class="board-item">
              <p [innerHTML]="item.description" class="slide-description"></p>
              <ion-list class="form-body">
                <form #form="ngForm" class="form">
                  <div *ngFor="let question of item.questions; let x = index">
                    {{ question.answers }}
                    <custom-input (value)="listenerAnswer($event, question._id, 0)" [id]="question._id" type="{{ question.type | lowercase }}"
                      [options]=question.answers title="{{ question.question }}"></custom-input>
                  </div>
                </form>
              </ion-list>
            </div>

When selecting one or several options from select and press OK, the following method is executed:

listenerAnswer(value, questionId, conditional?: boolean) {
        try {
          const data = JSON.parse(value.data);
          this.obj[value.id] = data.answer ? data.answer : data;
        } catch (e) {
          const data = value.data;
          this.obj[value.id] = data;
        }
        this.answers = {
          personId: this.personId,
          answers: this.obj,
          decision: this.decision,
        };
      }

See what happens when you submit the request: Information like: n and are added to the string and this is getting in the way Payload

In mongoDB the information is being saved with the n and :

Mongodb

Can you help me find a more suitable way to address this impasse?

Extra information:

At the time of selecting:

inputPopulate(data, id, target) {
    const value = data.toString();
    this.value.emit({
      id,
      data: value,
      target,
    });
  }

Console log do inputPopulaate

Method that performs the post.

sendAnswers(data): Observable<any> {

        let headers = new HttpHeaders().set('Content-Type', 'application/json');

        return this.http.post(`/answer`, data, headers );
      }
  • I suggest bringing this data before saving in Mongodb, take the \n and \". And why do you need the whole object as the value of each option? Just the _id is not enough?

  • So, I need the user response and I need to identify the Question for that answer. About treating before. I’m trying to do it but I can’t do it for regex.

  • Enter the code instead of images is much easier for someone to help you.

  • I updated the information. I hope you can help me find a viable path.

  • @Costamilam I solved the problem following its orientation. Now only _id is added.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.