gpt4 book ai didi

javascript - Ionic/Angular HttpClient 未获取类型数据 : error Property 'name' does not exist on type '{}'

转载 作者:行者123 更新时间:2023-12-01 02:37:21 26 4
gpt4 key购买 nike

在 Ionic 项目中,我尝试使用 HttpClient 从 API 获取数据,但 Angular 向我抛出类型“{}”上不存在属性“名称”。错误。其中有Component的代码以及组件调用的Provider方法。

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';


export class CreerDiscussionPage {
discussion: Discussion;

constructor(
public navCtrl: NavController,
public navParams: NavParams,
public rfcAPI: RfcApiProvider,
public discussionProvider: DiscussionProvider
) {
this.rfcAPI = rfcAPI;
this.discussionProvider = discussionProvider;
this.discussion = new Discussion();
}

getDiscussion() {
this.rfcAPI.getDiscussion()
.then(data => {
// Error raised in this place : Property 'name' does not exist on type '{}'.
console.log(data.name);
});
}

}

// Method in the rfcApi provider

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

.....

getDiscussion() {
return new Promise(resolve => {
this.http.get('http://localhost:3000/api/conversation/9a19d9e944e4b105d360614572b715434a365e724beddb83a4f1464a1ea79d29')
.subscribe(data => {
resolve(data);
}, err => {
console.log(err);
});
});

}

您知道如何定义 HttpClient 返回的 data 变量的类型吗?或者其他方法来解决我的问题?

最佳答案

.then((data: any) => {
console.log(data.name);
});

它可能像这样工作吗?

我认为你可以在你的服务中做到这一点

return this.http.get('http://localhost:3000/api/conversation/9a19d9e944e4b105d360614572b715434a365e724beddb83a4f1464a1ea79d29').toPromise()

关于javascript - Ionic/Angular HttpClient 未获取类型数据 : error Property 'name' does not exist on type '{}' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47801815/

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