Javascript TypeError:无法读取webservice上未定义的属性“then”
我有一个错误: TypeError:无法读取未定义的属性“then” 在FactoryMethod.ReadItems和SetStatus FactoryMethod.tsx:140:6 在FactoryMethod.componentDidMount FactoryMethod.tsx:77:10 调试器在此方法语句处中断:Javascript TypeError:无法读取webservice上未定义的属性“then”,javascript,json,reactjs,typescript,Javascript,Json,Reactjs,Typescript,我有一个错误: TypeError:无法读取未定义的属性“then” 在FactoryMethod.ReadItems和SetStatus FactoryMethod.tsx:140:6 在FactoryMethod.componentDidMount FactoryMethod.tsx:77:10 调试器在此方法语句处中断: // read items using factory method pattern and sets state accordingly private read
// read items using factory method pattern and sets state accordingly
private readItemsAndSetStatus(): void {
this.setState({
status: "Loading all items..."
});
const factory: ListItemFactory = new ListItemFactory();
factory.getItems(this.props.spHttpClient, this.props.siteUrl, this.props.listName)
.then((items: IListItem[]) => {
const keyPart: string = this.props.listName === "GenericList" ? "" : this.props.listName;
// the explicit specification of the type argument `keyof {}` is bad and
// it should not be required.
this.setState<keyof {}>({
status: `Successfully loaded ${items.length} items`,
["Details" + keyPart + "ListItemState"] : {
items
},
columns: buildColumns(items)
});
});
}
服务返回的json为:
[{
"Author": {
"Title": "Luis Valencia"
},
"Editor": {
"Title": "Luis Valencia"
},
"Id": 1,
"ID": 1,
"Title": "Generic List Item 1",
"Modified": "2017-10-23T20:02:22Z",
"Created": "2017-10-23T20:02:22Z"
},
{
"Author": {
"Title": "Luis Valencia"
},
"Editor": {
"Title": "Luis Valencia"
},
"Id": 2,
"ID": 2,
"Title": "Generic List Item 2",
"Modified": "2017-11-07T17:52:34Z",
"Created": "2017-11-07T17:52:34Z"
}
]
调试时,我注意到JSON映射显然不起作用。
请参见屏幕截图,项目未定义
您没有从getItems返回请求者。在每个case语句中,在请求者之前添加一个return语句
return requester.get(`${siteUrl}/_api/web/lists/getbytitle('${listName}') ...
.then是一个方法Promise和Promise-like对象,由于getItems在默认情况下返回undefined或null,因此它会抛出一个错误。getItems函数中的任何开关分支都不会返回任何内容。你需要实际返回承诺:所以返回请求者。获取……然后,@Duncan说了什么,你的默认案例也需要返回承诺。请进一步说明,我不是本地专家,所以不知道如何修复它不确定我是否理解,你能更详细地说明我需要修复的地方和方法吗?例如:案例公告:返回请求者。获取。。。getItems不知道在没有针对每个开关情况的返回语句的情况下返回给它的调用者的内容。我得到以下编译错误:[ts]类型“Promise”不可分配给类型“Promise”。类型“void”不可分配给类型“IListItem[]”。方法Promise.thenoncompleted?:值:{value:IListItem[];}=>void |表,onRejected?:错误:any=>void |表:Promise+2重载
[{
"Author": {
"Title": "Luis Valencia"
},
"Editor": {
"Title": "Luis Valencia"
},
"Id": 1,
"ID": 1,
"Title": "Generic List Item 1",
"Modified": "2017-10-23T20:02:22Z",
"Created": "2017-10-23T20:02:22Z"
},
{
"Author": {
"Title": "Luis Valencia"
},
"Editor": {
"Title": "Luis Valencia"
},
"Id": 2,
"ID": 2,
"Title": "Generic List Item 2",
"Modified": "2017-11-07T17:52:34Z",
"Created": "2017-11-07T17:52:34Z"
}
]
return requester.get(`${siteUrl}/_api/web/lists/getbytitle('${listName}') ...