как избежать дублирования записей в магазине акита?

Перед отправкой API-запроса на вставку я хочу проверить, существует ли элемент в магазине или нет, если нет, то только отправить запрос на вставку.

add(){
   const item = {id: 4,name:'test1',description:'description'}

   // here i want to check whether test1 already exist or not

   this.store.add(item);
   this.httpService.insert(item).subscribe(
   result => {
    this.messageService.handleSuccess('inserted');
    this.store.updateId(id, result.id);
  },
  error => this.messageService.handleError('error on insert:', error)
);

может кто-нибудь помочь?


person yog    schedule 11.05.2020    source источник


Ответы (1)


Вы можете использовать запрос:

add(){
   const item = {id: 4,name:'test1',description:'description'}

   if(query.hasEntity(4)) {
     // exists
   }

   this.store.add(item);
   this.httpService.insert(item).subscribe(
   result => {
    this.messageService.handleSuccess('inserted');
    this.store.updateId(id, result.id);
  },
  error => this.messageService.handleError('error on insert:', error)
);
person undefined    schedule 20.05.2020