简体   繁体   English

Angular HTTP 删除请求,应用更新问题

[英]Angular HTTP delete request, app updating question

Link to stackblitz editor: https://stackblitz.com/edit/github-krcuye链接到 stackblitz 编辑器: https ://stackblitz.com/edit/github-krcuye

Link to API needed to have full test functionality: https://github.com/TSPeterson206/conspiracyAPI具有完整测试功能所需的 API 链接: https : //github.com/TSPeterson206/conspiracyAPI

API uses knex and seeds would need to be run via: npm run knex seed:run...due to items being deleted onclick. API 使用 knex 并且种子需要通过以下方式运行:npm run knex seed:run...由于项目在点击时被删除。

I am attempting to use an HTTP delete request to remove an item out of an array and then make an HTTP get request to grab the now changed array for continued use in the app.我正在尝试使用 HTTP 删除请求从数组中删除项目,然后发出 HTTP get 请求以获取现在更改的数组以继续在应用程序中使用。 I can delete the item and it appears to leave the database.我可以删除该项目,它似乎离开了数据库。 The element will disappear and I can use postman to see that it is gone.该元素将消失,我可以使用邮递员查看它已消失。 The app doesn't seem to reflect the absence until it is reloaded.该应用程序在重新加载之前似乎没有反映缺席。 I call another function within the function that is used to delete and this deleted item shows as present (toggleData).我在用于删除的函数中调用另一个函数,这个删除的项目显示为存在 (toggleData)。 I need the app to update in real-time as I display {{allNouns.length}} in the splash page.当我在启动页面中显示 {{allNouns.length}} 时,我需要应用程序实时更新。 I am using the httpClientModule and tap/concatMap.我正在使用 httpClientModule 和 tap/concatMap。 I have toyed around with async/await and setTimeout but so far no luck.我玩过 async/await 和 setTimeout 但到目前为止还没有运气。 Any information on order of operations or how to best seeing realtime results would be much appreciated.任何有关操作顺序或如何最好地查看实时结果的信息将不胜感激。

Just for context.只是为了上下文。 this.userNounsHolder is an array of objects used for a dashboard that allows the deletion. this.userNounsHolder 是用于允许删除的仪表板的对象数组。 this.allNouns is an array of strings that are displayed in the main page. this.allNouns 是一个显示在主页面上的字符串数组。 3 checkboxes (handled by toggleData) determine what strings are in the array. 3 个复选框(由 toggleData 处理)确定数组中的字符串。 A bunch of HTTP calls are made in ngOninit, but those are handling generation fine and seem unrelated to this particular issue.在 ngOninit 中进行了一堆 HTTP 调用,但这些调用处理得很好,似乎与这个特定问题无关。 Thank you for the sets of eyes.谢谢你的眼睛。

At present I am just trying to get this work for nouns.目前我只是想为名词做这项工作。 Click on modal to see the dashboard in question.单击模态以查看有问题的仪表板。

FUNCTION USED TO DELETE用于删除的函数

delete(idNum){this.http.delete(`http://localhost:8000/nouns/${idNum}`).pipe(
  concatMap(user => this.http.get(`http://localhost:8000/nouns/${this.user[0].id}`)),
  tap(result => this.userNounsHolder = result)).subscribe(
    this.userNouns = this.userNounsHolder.map(noun => noun.content)
  )
  // console.log('usernouns',this.userNouns, this.allNouns)
  this.toggleData('nouns')
}

HTML containing delete function包含删除功能的 HTML

<jw-modal id="custom-modal-1">
  <div id="dashboardTop">
  <h1 class="modalHeader" style="height:100px">Nouns Dashboard</h1>
  <button class="modalClose" (click)="closeModal('custom-modal-1');">X</button>
</div>
<div *ngFor="let noun of userNounsHolder" class="modalLoop">
  <div class="nounLoop">{{noun.content}}</div>
  <button (click)="delete(noun.id)">X</button>
</div>
</jw-modal>

TS of toggleData function toggleData 函数的 TS

toggleData(value) {
if(value==='nouns'){
  console.log('hitting toggledata nouns before', this.userNouns);
    let nounpre=[];
    let nounuser=[];
    let nounalluser=[];

    this.nounBooleans[0] ? nounpre = this.stockNouns:null;
    this.nounBooleans[1] ? nounuser = this.userNouns:null;
    this.nounBooleans[2] ? nounalluser = this.onlyUserNouns : null;
    this.allNouns = nounpre.concat(nounuser).concat(nounalluser);
    console.log('hitting toggledata nouns after', this.userNouns);

}

if(value==='verbs'){
  console.log('hitting toggledata verbs');

  let verbpre=[];
  let verbuser=[];
  let verballuser=[];

  this.verbBooleans[0] ? verbpre = this.stockVerbs:null;
  this.verbBooleans[1] ? verbuser = this.userActions:null;
  this.verbBooleans[2] ? verballuser = this.onlyUserVerbs : null;
  this.allVerbs = verbpre.concat(verbuser).concat(verballuser);
}

if(value==='descriptors'){
  console.log('hitting toggledata descriptors');

  let descriptorpre=[];
  let descriptoruser=[];
  let descriptoralluser=[];

  this.descriptorBooleans[0] ? descriptorpre = this.stockDescriptors:null;
  this.descriptorBooleans[1] ? descriptoruser = this.userDescriptors:null;
  this.descriptorBooleans[2] ? descriptoralluser = this.onlyUserDescriptors : null;
  this.allDescriptors = descriptorpre.concat(descriptoruser).concat(descriptoralluser);
}


  }

Figured it out.弄清楚了。 Required a different approach.需要不同的方法。 Used async/await and toPromise() and it works as intended.使用 async/await 和 toPromise() 并且它按预期工作。 Learned some stuff abouot promises and angular and stackblitz in the mean time though.同时也学到了一些关于 promises、angular 和 stackblitz 的东西。 code used is below:使用的代码如下:

async delete(idNum,type){
await this.http.delete(`http://localhost:8000/${type}/${idNum}`).toPromise()
if(type==='nouns'){
this.userNouns= await 
this.http.get(`http://localhost:8000/${type}/${this.user[0].id}`).toPromise()
this.userNounsHolder=this.userNouns;
this.userNouns=this.userNouns.map(ele=>ele.content);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM