Angular+Express查询数据库遇isProxied未定义错误求解决
解决“Unhandled Promise rejection: Cannot read properties of undefined (reading 'isProxied')”问题
结合你提供的前后端代码,这个错误主要由几个细节问题导致,以下是逐个修复方案:
1. 前端请求参数不匹配 + 语法错误
后端接口/description-by-id通过req.query.param1获取ID,但你前端请求里用的是parameter1,参数名完全不对,导致后端拿到的ID是undefined,findById(undefined)会触发Promise异常。同时请求的模板字符串未正确闭合,还多了空格。
修复后的请求代码:
this.http.get<any>(`http://localhost:8100/description-by-id?param1=${item.descriptionId}`)
2. 组件构造器错误注入模块
ScrollingModule是Angular的模块,只能在对应NgModule的imports数组中导入,不能当成服务注入到组件构造器中,这会导致依赖注入出错,直接引发isProxied相关的异常。
修复构造器:
constructor(private http: HttpClient) {}
同时在组件所属的模块里添加导入:
import { ScrollingModule } from '@angular/cdk/scrolling'; @NgModule({ imports: [ // ...其他已导入模块 ScrollingModule ] }) export class YourFeatureModule {}
3. 后端路由未处理异常
后端的findById操作没有捕获Promise错误,当查询失败时会抛出未处理的Promise rejection,这个异常会传递到前端,触发你看到的错误。
修复后端路由:
router.get('/description-by-id', (req, res) => { let descriptionId = req.query.param1; Description.findById(descriptionId) .then(description => res.json(description)) .catch(err => { console.error('查询描述失败:', err); res.status(500).json({ error: '查询失败', message: err.message }); }); });
4. 前端Input空值安全处理
@Input() lists可能在组件初始化时还未传入,直接访问this.lists.descriptions会导致undefined错误,虽然和当前报错不一定直接相关,但必须做空值保护避免后续问题:
ngOnInit() { // 使用可选链操作符处理空值 if (this.lists?.descriptions?.length > 0) { for (let item of this.lists.descriptions) { this.http.get<any>(`http://localhost:8100/description-by-id?param1=${item.descriptionId}`) .subscribe({ next: (result: any) => { this.descriptionsArr.push({ id: item.descriptionId, name: result.name, surname: result.surname }); }, error: (err) => { console.error('获取描述详情失败:', err); } }); } } }
5. 添加请求错误处理
原订阅代码没有处理请求失败的情况,当后端返回异常时会触发未捕获的订阅错误,上面的代码已经添加了error回调来捕获并处理这类错误。
内容的提问来源于stack exchange,提问作者Nikita Kononov
相关产品推荐
相关产品推荐

