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 []; } }
额外注意事项
- 确保
getCurrentUser的返回类型定义为Promise<User | null>,其中User类型与你的Prisma模型匹配,保证TypeScript能正确推断类型。 - 原代码存在语法不完整问题(
catch块未闭合、函数无返回值),修正时已一并补全,避免运行时错误。
内容的提问来源于stack exchange,提问作者IGODX
相关产品推荐
相关产品推荐

