简体   繁体   English

自定义 Object 返回错误:类型“void”上不存在属性“forEach”.ts(2339)

[英]Custom Object Returning Error: Property 'forEach' does not exist on type 'void'.ts(2339)

I have a variable that will either be of a class User or void , the only problem is that when I run this code it gives me an error of: Property 'forEach' does not exist on type 'void'.ts(2339) .我有一个变量,它要么是 class User要么是void ,唯一的问题是当我运行这段代码时它给我一个错误: Property 'forEach' does not exist on type 'void'.ts(2339) I have tried to work around this a hundred different ways, but nothing works.我尝试过一百种不同的方法来解决这个问题,但没有任何效果。

let friends: [User] | void = await this.getFriendsPosts(host);
friends.forEach(element => {

});

Update:更新:

Here is my code for the function this.getFriendsPosts(host) and other subsequent functions:这是我的 function this.getFriendsPosts this.getFriendsPosts(host)和其他后续函数的代码:

async getFriendsPosts(host: User){
 let userFriendsID = host.friends
 let userFriends = []
 let retrievedPosts: [Post[]?] = []
 userFriendsID.map(async uid =>{
    let val = new User(await this.checkFriend(uid))
    userFriends.push(val)
    console.log(userFriends)
  })
 }


//*convert to users with promises
async checkFriend(uid){
  let metaData;
  let postInterface= [];
  let friendInterface : unknown;
  //*getting the post history
  let promise1 = new Promise((res, rej) => {
    metaData = 
this.firestore.collection('Users').doc(uid.toString())
    let postData = metaData.collection('Posts')
    postData.valueChanges().subscribe(vals =>{
      vals.forEach(input => {
        postInterface.push({
          title: input.title,
          id: input.id,
          user: input.user,
          timebomb: input.timebomb,
          type: input.type,
          timestamp: input.timestamp
        } as complexPostInterface)      
      })
      res(vals)
    })
  })
  //*getting the user stats
  let promise2 = new Promise((res, rej) => {
    metaData.valueChanges().subscribe(vals =>{
      friendInterface = {
        name: vals.name,
        uid: vals.uid,
        nickname: vals.nickname,
        currentPosts: postInterface,
        status: new Post(postInterface[0] as any),
        timestamp: vals.timestamp,
      } as complexUserObj
      res(friendInterface as complexUserObj)
    })
  })
  await Promise.all([promise1,promise2])
  console.log(friendInterface)
  return (friendInterface)
}

Your getFriendsPosts method should return undefined instead of void.您的 getFriendsPosts 方法应该返回 undefined 而不是 void。 Void should be used in functions whose return value will be ignored. Void 应该用在返回值将被忽略的函数中。

Property 'forEach' does not exist on undefined either, so you'll need to check first that friends exist:属性 'forEach' 也不存在于 undefined 上,因此您需要首先检查 friends 是否存在:

let friends: [User] | undefined = await this.getFriendsPosts(host);
if (friends) {
  friends.forEach(element => {

  });
}

Or alternatively make the method return an empty array of Users and you don't need to check for it.或者让该方法返回一个空的 Users 数组,您不需要检查它。

暂无
暂无

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

相关问题 错误TS2339:类型“ {}”上不存在属性“ forEach” - error TS2339: Property 'forEach' does not exist on type '{}' forEach Typescript TS2339“类型&#39;void&#39;上不存在” - forEach Typescript TS2339 “does not exist on type 'void'” TypeScript:TS2339 错误 — 类型“对象”上不存在属性 - TypeScript: TS2339 error — Property does not exist on type 'object' 使用 Typescript 反应自定义 Hook (({目标}:任何)=&gt;无效)'.ts(2339)” - React Custom Hook with Typescript Type error “Property 'x' does not exist on type 'interface | (({ target }: any) => void)'.ts(2339)” 类型&#39;void&#39;.ts(2339)上不存在属性&#39;subscribe&#39;。我收到此错误 - Property 'subscribe' does not exist on type 'void'.ts(2339).Am getting this error 错误TS2339:类型“ PromiseLike”上不存在属性“ catch” <void> &#39; - error TS2339: Property 'catch' does not exist on type 'PromiseLike<void>' 错误 TS2339:属性 &#39;finally&#39; 在类型 &#39;Promise 上不存在<void> &#39; - error TS2339: Property 'finally' does not exist on type 'Promise<void>' TS2339 错误:“属性‘forEach’在类型‘集合’上不存在<string, guildmember> “ DiscordJS</string,> - TS2339 Error: "Property 'forEach' does not exist on type 'Collection<string, GuildMember>" DiscordJS Angular 管道上的 .includes 返回“属性 &#39;includes&#39; 在类型 &#39;never&#39;.ts(2339)&#39; 上不存在错误消息 - .includes on Angular pipe is returning 'Property 'includes' does not exist on type 'never'.ts(2339)' error message TS2339:类型“对象”上不存在属性“地图” - TS2339: Property 'map' does not exist on type 'Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM