[英]Typescript: No index signature with a parameter of type 'string' was found on type '{}'
So I have the following object:所以我有以下对象:
{"1": {"name": "spencer", "number": "965756"}}, {"2": {"name": "mary", "number": "5346"}}, {"3": {"name": "john", "number": "1234"}}
This object is the result of a database query and I would like to convert this object into an array, using the array.push()
method.此对象是数据库查询的结果,我想使用
array.push()
方法将此对象转换为数组。 This gives the error:这给出了错误:
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
I was so sure that the error occurs, because '{}'
would be the empty object in case the database query failed.我非常确定会发生错误,因为如果数据库查询失败,
'{}'
将是空对象。 So I did a check if the result object ( plainObjectResult
) was empty before converting it into an array.因此,在将结果对象(
plainObjectResult
)转换为数组之前,我检查了它是否为空。 But the error stays and I don't have any idea why.但错误仍然存在,我不知道为什么。
Here is the code:这是代码:
export interface Person {
name: string;
number: string;
}
export interface PersonResults {
[key: string]: Person;
}
// make the Realm database Result Object a plain "normal" object
const plainObjectResult: object = realmToPlainObject(queryResult);
const resultArray: PersonResults[] = [];
if (Object.keys(plainObjectResult).length > 0) {
for (let person in plainObjectResult) {
// person: 0, 1, 2, ...
// plainObjectResult[person]: {"name": "spencer", "number": "965756"}
resultArray.push({[person]: plainObjectResult[person]}); // above error occurs for "plainObjectResult[person]"
}
}
Can someone please help me?有人可以帮帮我吗?
This is INVALID {"1": {"name": "spencer", "number": "965756"}}, {"2": {"name": "mary", "number": "5346"}}, {"3": {"name": "john", "number": "1234"}}
这是无效的
{"1": {"name": "spencer", "number": "965756"}}, {"2": {"name": "mary", "number": "5346"}}, {"3": {"name": "john", "number": "1234"}}
It should have been { "1": { "name": "spencer", "number": "965756" }, "2": { "name": "mary", "number": "5346" }, "3": { "name": "john", "number": "1234" } }
应该是
{ "1": { "name": "spencer", "number": "965756" }, "2": { "name": "mary", "number": "5346" }, "3": { "name": "john", "number": "1234" } }
Try this尝试这个
export interface Person {
name: string;
number: string;
}
export interface PersonResults {
[key: string]: Person;
}
// make the Realm database Result Object a plain "normal" object
let plainObjectResult: any = { "1": { "name": "spencer", "number": "965756" }, "2": { "name": "mary", "number": "5346" }, "3": { "name": "john", "number": "1234" } };
const resultArray: PersonResults[] = [];
if (Object.keys(plainObjectResult).length > 0) {
for (let person in plainObjectResult) {
let id = '' + person;
let obj:PersonResults = {};
obj[id] = plainObjectResult[person];
resultArray.push(obj); // above error occurs for "plainObjectResult[person]"
}
}
console.log('resultArray: ', resultArray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.