You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报错:Promise类型不存在favoriteIds属性,求解决方案

问题解决:Property 'favoriteIds' does not exist on type 'Promise<...>'

核心原因

getCurrentUser() 是异步函数,返回的是Promise对象而非直接的用户实体。你直接赋值得到的currentUser是Promise实例,尝试访问currentUser.favoriteIds时,实际是在访问Promise的属性,而Promise本身并没有这个属性,因此TypeScript抛出错误。

修正代码

只需对getCurrentUser()添加await关键字,等待Promise解析为实际用户对象后再使用:

export default async function getFavoriteListings() {
  try {
    // 关键:添加await等待Promise解析
    const currentUser = await getCurrentUser();
    if (!currentUser) return [];
    
    const favorites = await prisma.listing.findMany({
      where: {
        id: {
          in: [...(currentUser.favoriteIds || [])]
        }
      }
    });
    return favorites;
  } catch (error) {
    console.error('获取收藏列表失败:', error);
    return [];
  }
}

额外注意事项

  1. 确保getCurrentUser的返回类型定义为Promise<User | null>,其中User类型与你的Prisma模型匹配,保证TypeScript能正确推断类型。
  2. 原代码存在语法不完整问题(catch块未闭合、函数无返回值),修正时已一并补全,避免运行时错误。

内容的提问来源于stack exchange,提问作者IGODX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:51:23