'Assign a value to a [(ngModel)] input

new to MEAN stack here,

I'm trying to insert datas into MongoDB, but with the _id of the connected user, so I have a function which display it :

  getSessionInfos(){
    return sessionStorage.getItem('id'); 
  }
db.collection('transactions').insertOne({
       date : req.body.date,
       titre: req.body.titre,
       description : req.body.description,
       montant : "-" + req.body.montant,
       id: req.body.id, <-------------
       type: "debit"

So I thought to myself, alright, I'm gonna create a hidden input with the ID as a value, it's gonna be easier :

<input type="text" [(ngModel)]="data.id" value="getSessionInfos()">

Except that NO, it seems that you can't change the value of a NGmodel input, So how am I supposed to do that ?

Thanks a lot



Solution 1:[1]

data: any = {
  date: "",
  titre: "",
  description: "",
  montant: "",
  id: sessionStorage.getItem('id'),
  type: ""
}

Solution 2:[2]

<input [hidden]='true' type="text" [(ngModel)]="data?.id" >

Solution 3:[3]

<ng-container *ngfor="let dataa in data"><input [hidden]='true' type="text" [(ngModel)]="dataa.id" (change)="getSessionInfos()"></ng-container>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Antoine Pascual
Solution 2 Zrelli Majdi
Solution 3 pringi