简体   繁体   English

类型为forEach的属性在类型void上不存在

[英]property of type forEach does not exist on type void

I have seen questions like this for example:( forEach Typescript TS2339 "does not exist on type 'void'" ) 我已经看到了这样的问题,例如:( forEach Typescript TS2339“在'void'类型上不存在”

But I still having difficulty resolving my specific problem. 但是我仍然很难解决我的特定问题。

ngOnInit() {

      var __this = this;

      this.historianService.getHistorian()
      .then(function(res){
          console.log(res)

          res.forEach(transaction => {
              __this.results.push(res);

          })
  })
 }

console.log(res) gives me the following result: 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

As you can see the returned variable res has the function forEach associated to it. 如您所见,返回的变量res具有与forEach关联的功能。 I'm confused then, why I cant then do: 那我很困惑,为什么我不能这样做:

res.forEach(transaction => {
          __this.results.push(res);

      })

As I'm getting the following error when I do: 当我这样做时出现以下错误:

Property 'forEach' does not exist on type 'void | 属性'forEach'在类型'void |上不存在。 Response'. 响应'。 Property forEach does not exist on type 'void' 属性forEach在'void'类型上不存在

EDIT: getHistorian function: 编辑: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
    });

I think that might the issue coz of return inside ngOnInit , 我认为这可能是ngOnInitreturn的问题,

Change your code like this and try again : 像这样更改代码,然后重试:

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);
        });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM