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

survey-react出现this.addEvent is not a function报错排查

问题根因

抛出this.addEvent is not a function错误的核心原因是:你调用Survey.Model创建问卷模型实例时,遗漏了new关键字。
Survey.Model是构造函数,直接以普通函数形式调用时,函数内部的this不会绑定到新生成的实例对象,无法访问原型链上定义的addEvent方法,直接触发类型错误。

修复步骤
  • 修改src/index.js中实例化模型的代码,补充new关键字,修正后的完整代码如下:
import React from 'react';
import ReactDOM from 'react-dom';
import * as Survey from 'survey-react';
const questions = {
  elements:[{"type":"comment","name":"If you could enhance one thing about this product, what would it be?","isRequired":true}]
};
// 补充new关键字,正确实例化模型
const srv = new Survey.Model(questions);
srv.data = {"If you could enhance one thing about this product, what would it be?":"I like it as is, no changes needed."};
ReactDOM.render(<Survey.Survey model={srv} />, document.getElementById('root'));
  • 修正public/index.html的语法错误,你贴的代码末尾</html标签缺失闭合的>,补全后如下:
<!DOCTYPE html>
<html lang="en">
  <body>
    <div id="root"></div>
  </body>
</html>
验证说明

你当前使用的survey-react@1.9.33和React 16版本、react-scripts 2.x版本完全兼容,没有依赖冲突问题。修改完上述两处代码后,重新执行npm start启动项目,访问http://localhost:3000即可正常渲染问卷,不会再出现该报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 21:30:47