gpt4 book ai didi

javascript - 将从 API 检索的数据存储在另一个 API - Ionic

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:52 25 4
gpt4 key购买 nike

我是 Ionic 的初学者。我有 2 个用于表单的 API。第一个 API 用于检索用户的名称。我能够检索名称并将其显示在标签中。

代码如下:

我的提供商(第一个 API)

getStaff(){
return this.http.get<StaffRecords>(`${this.staffApiUrl}//some api,
{headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN'))});}

我页面的.ts文件(第一个API)

ionViewDidLoad(){

this.christmasProvider.getStaff().pipe(
map((staffResult: StaffRecords) => staffResult && staffResult.records)
).subscribe(staffed => {
if (staffed){
this.storage.set('staff', staffed);
this.staff = staffed;
console.log(staffed);
}
});}

我在此表单中使用的第二个 API 用于发布用户输入。目前,我能够将用户输入数据发布到 API 中。但是,我不知道如何获取从第一个 API 检索到的数据并将其与所有其他字段一起发布到我的第二个 API 中。基本上,我需要使用从我的第一个 API(称为“display_name”)中检索到的数据并将其发布到我的第二个 API(称为“dedicated_by_name”)中的字段中。

代码如下:我的提供商(第二个 API)

updateDedication(pledge_amount: number, phone_no: string, sing_to: string, dedicated_datetime: string, sing_to_department: string, location: string, song: string, other_song_request: string, special_request: string, message: string, remain_anon: number, status: string){
return Observable.create(observer =>
this.http.post(`${this.dataApiUrl}//some api`,
{ records: [{
//"dedicated_by_name": dedicated_by_name,
"pledge_amount": pledge_amount,
"phone_no": phone_no,
"sing_to": sing_to,
"dedicated_datetime": dedicated_datetime,
"sing_to_department": sing_to_department,
"location": location,
"song": song,
"other_song_request": other_song_request,
"special_request": special_request,
"message": message,
"remain_anon": remain_anon,
"status": status}],
resource_id: '//some api' },
{ headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN')),
responseType: 'text'})
.subscribe(data => {
console.log(data);
observer.next(true);
observer.complete();
}, error => {
console.log(error);
observer.next(false);
observer.complete();
})
);}

我页面的 .ts 文件(第二个 API)

gotoConfirmpage(){
this.submitAttempt = true;
console.log("Success!");
console.log(this.dediForm2.value);

var obj = this.dediForm2.value;
//console.log(dedicated_by_name);

this.christmasProvider.updateDedication(obj.pledge_amount, obj.phone_no, obj.sing_to, obj.dedicated_date+"T"+obj.dedicated_time+":00", obj.sing_to_department, obj.location, obj.song, obj.other_song_request, obj.special_request, obj.message, obj.remain_anon, this.status)
.subscribe((allowed => {
//this.submit_error = false;
//this.submit_success = true;
console.log(allowed);
//console.log(obj.dedicated_by_name);
}
));

//push to confirm page
this.navCtrl.push(SongDediCfmPage);}

表单的 HTML(我包含了第一个 api 的部分和第二个 api 的第一个字段)

第一个 API 的 HTML

<ion-content padding>
<p *ngIf="submitAttempt" style="color: #ea6153">Please fill out all fields!</p>
<form [formGroup]="dediForm2">
<h6 style="font-weight: bold" id="dedititle">My Dedication</h6>
<ion-card color="secondary">
<ion-list>
<ion-item>
<ion-icon item-left name="ios-person-outline" style="font-size:30px; width: 20px;"></ion-icon>
<ion-label *ngFor="let s of staff" style="font-style: italic; font-size: 15px;">{{ s.display_name }}</ion-label>
</ion-item>

第二个 API 的 HTML(仅第一个字段 + 提交按钮)

<ion-item>
<ion-icon item-left name="ios-cash-outline" style="font-size:30px; width: 20px;"></ion-icon>
<ion-input type="number" formControlName="pledge_amount" name="pledge_amount" [class.invalid]="!dediForm2.controls.pledge_amount.valid && (dediForm2.controls.pledge_amount.dirty || submitAttempt)" placeholder="Pledge Amount" style="font-style:italic; font-size: 15px;"></ion-input>
</ion-item>

<ion-item *ngIf="!dediForm2.controls.pledge_amount.valid && (dediForm2.controls.pledge_amount.dirty || submitAttempt)">
<p>Please enter an amount lesser than this.</p>
</ion-item>

<button ion-button icon-start class="pledgebtn" (click)="gotoConfirmpage()" [disabled]="!dediForm2.valid">
<img src="assets/imgs/smiley_final.png" class="smileyimg">
DEDICATE <br>SONG
</button>
</form>

请指导我如何存储从第一个 API 检索到的值并将其发回第二个 API。谢谢大家。

最佳答案

我们可以使用 switchMap 移动两个服务的 HTTP 调用并合并最终结果。否则我们需要遵循这种简单的方法作为

getStaff(){

this.christmasProvider.getStaff().pipe(

map((staffResult: StaffRecords) => staffResult && staffResult.records)
).subscribe(staffed => {
if (staffed){
this.storage.set('staff', staffed);
this.staff = staffed;

this.christmasProvider.updateDedication(obj.pledge_amount, obj.phone_no, obj.sing_to, obj.dedicated_date+"T"+obj.dedicated_time+":00", obj.sing_to_department, obj.location, obj.song, obj.other_song_request, obj.special_request, obj.message, obj.remain_anon, this.status, staffed)
.subscribe((allowed => {
console.log(staffed);
}
});}

关于javascript - 将从 API 检索的数据存储在另一个 API - Ionic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com