我见过这样的问题:(forEach Typescript TS2339 "does not exist on type 'void'")
但我仍然难以解决我的具体问题。
ngOnInit() {
var __this = this;
this.historianService.getHistorian()
.then(function(res){
console.log(res)
res.forEach(transaction => {
__this.results.push(res);
})
})
}
console.log(res)给出了以下结果:
(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "1dec5c91d047deebb588d69e4844ccdda03b3621783bc6c8c82b286e6e4c3d65", transactionType: "org.hyperledger.composer.system.IssueIdentity", transactionInvoked: "resource:org.hyperledger.composer.system.IssueIden…bb588d69e4844ccdda03b3621783bc6c8c82b286e6e4c3d65", participantInvoking: "resource:org.hyperledger.composer.system.NetworkAdmin#admin", …}
1
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "2114019f-7179-4f0f-b0fc-bc25d116f2eb", transactionType: "org.hyperledger.composer.system.ActivateCurrentIdentity", transactionInvoked: "resource:org.hyperledger.composer.system.ActivateC…rentIdentity#2114019f-7179-4f0f-b0fc-bc25d116f2eb", identityUsed: "resource:org.hyperledger.composer.system.Identity#…f1adb8973507f6c7316039a49d92ab007abccf3a882b7dad3", …}
2
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "402de9fd0f8fafc6fa63d0785f646828e1266baf94228b95275fd12eb0cdf745", transactionType: "org.blockaviation.OurSetupDemo", transactionInvoked: "resource:org.blockaviation.OurSetupDemo#402de9fd0f…6fa63d0785f646828e1266baf94228b95275fd12eb0cdf745", participantInvoking: "resource:org.hyperledger.composer.system.NetworkAdmin#admin", …}
3
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "685dc153-bb39-4b73-836b-faf3db3d3b0c", transactionType: "org.hyperledger.composer.system.StartBusinessNetwork", transactionInvoked: "resource:org.hyperledger.composer.system.StartBusinessNetwork#685dc153-bb39-4b73-836b-faf3db3d3b0c", eventsEmitted: Array(0), …}
4
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "685dc153-bb39-4b73-836b-faf3db3d3b0c#0", transactionType: "org.hyperledger.composer.system.AddParticipant", transactionInvoked: "resource:org.hyperledger.composer.system.AddParticipant#685dc153-bb39-4b73-836b-faf3db3d3b0c%230", eventsEmitted: Array(0), …}
5
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "685dc153-bb39-4b73-836b-faf3db3d3b0c#1", transactionType: "org.hyperledger.composer.system.IssueIdentity", transactionInvoked: "resource:org.hyperledger.composer.system.IssueIdentity#685dc153-bb39-4b73-836b-faf3db3d3b0c%231", eventsEmitted: Array(0), …}
6
:
{$class: "org.hyperledger.composer.system.HistorianRecord", transactionId: "6a1d4203-6c9e-4756-89e3-2df39d505421", transactionType: "org.hyperledger.composer.system.ActivateCurrentIdentity", transactionInvoked: "resource:org.hyperledger.composer.system.ActivateC…rentIdentity#6a1d4203-6c9e-4756-89e3-2df39d505421", identityUsed: "resource:org.hyperledger.composer.system.Identity#…d78f39e21ec879210cdff2f8506d5f326af245563ebfcbb69", …}
length
:
7
__proto__
:
Array(0)
concat
:
ƒ concat()
constructor
:
ƒ Array()
copyWithin
:
ƒ copyWithin()
entries
:
ƒ entries()
every
:
ƒ every()
fill
:
ƒ fill()
filter
:
ƒ filter()
find
:
ƒ find()
findIndex
:
ƒ findIndex()
forEach
:
ƒ forEach()
includes
:
ƒ includes()
indexOf
:
ƒ indexOf()
join
:
ƒ join()
keys
:
ƒ keys()
lastIndexOf
:
ƒ lastIndexOf()
length
:
0
map
:
ƒ map()
pop
:
ƒ pop()
push
:
ƒ push()
reduce
:
ƒ reduce()
reduceRight
:
ƒ reduceRight()
reverse
:
ƒ reverse()
shift
:
ƒ shift()
slice
:
ƒ slice()
some
:
ƒ some()
sort
:
ƒ sort()
splice
:
ƒ splice()
toLocaleString
:
ƒ toLocaleString()
toString
:
ƒ toString()
unshift
:
ƒ unshift()
values
:
ƒ values()
Symbol(Symbol.iterator)
:
ƒ values()
Symbol(Symbol.unscopables)
:
{copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}
__proto__
:
Object
正如您所看到的,返回的变量res具有与之关联的函数forEach。我很困惑,为什么我不能这样做:
res.forEach(transaction => {
__this.results.push(res);
})
当我这样做时,我收到以下错误:
财产' forEach'类型' void |中不存在响应&#39 ;.属性forEach在类型' void'上不存在
编辑:getHistorian函数:
getHistorian(){
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({headers: headers});
var __this = this;
return this.http.get('http://'+ this.ip +':3000/api/system/historian?access_token=' + this.access_token,options)
.toPromise()
.then(function(res){
res = res.json();
return res;
})
.catch(e => {console.log(e); //print error if there is one
});
答案 0 :(得分:0)
我认为问题可能是return
{/ 1}}中的ngOnInit
问题
更改您的代码,然后重试:
this.historianService.getHistorian() // remove return from here as you are calling inside ngOnInit
.then((res) => { // use fatarrow inplace of normal function
console.log(res);
res.forEach(transaction => {
this.results.push(res);
});
})