Z
Z
zachfischer2018-07-27 13:32:35
Angular
zachfischer, 2018-07-27 13:32:35

How to automatically assign a record the ID of the user who creates it?

There are such models;
user.model:

export class User{
    constructor(
        public id?:number,
...
    ) { }
}

notepad.model:
export class Notepad{
    constructor(
        public id?:number,
        public user_id?:number,
        public title?:string,
        public description?:text
    ) { }
}

And I want to make sure that when a user creates a new record, user_id is automatically assigned to the user who creates it.
At the moment there is this form:
<form  #notepadForm='ngForm'>
  <div class="row">
   <label>Наименование</label>
    <input type="text"  id="title" #title='ngModel' [(ngModel)]='notepad.title' name="title" >
  </div>
  <div class="row">
   <label>Заметка</label>
   <textarea type="text" [(ngModel)]="notepad.description" id="description" name="description" #description></textarea>
  </div>   
</form>
<button type="submit"   (click)="saveNotepad(notepads)">Создать</button>

And such a script for creating a new record.
saveNotepad(notepads: Notepad) {
    this.servNotepad.createNotepad(this.notepda).subscribe(
       data => {
          console.log(data);
       }
    )
  }

But the problem is that because of this, you need to create a select to select a user in order to assign an id to the user_id field. Otherwise, it will give an error that the field is not filled. So, how can user.id be automatically assigned to notepad.user_id depending on which user creates the entry?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin, 2018-07-29
@Junart1

Initialize a new identifier in the constructor and that's it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question