surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.02k stars 785 forks source link

The Uncaught TypeError: value.setOwner is not a function exception is thrown when adding a new object to the survey.calculatedValues property #8222

Closed JaneSjs closed 2 months ago

JaneSjs commented 2 months ago

T17997 - Modify calculated values https://surveyjs.answerdesk.io/internal/ticket/details/T17997


Creating an array of calculated values throws an exception.

survey.calculatedValues = [{ name: "Foo", expression: "bar" }];

or

survey.calculatedValues.push({ name: "Foo", expression: "bar" });

View Demo

VM313 react-dom.production.min.js:198 Uncaught TypeError: value.setOwner is not a function
    at VM315 survey.core.js:59891:19
    at ./src/base.ts.Base.setArray (VM315 survey.core.js:2749:21)
    at ./src/base.ts.Base.setArrayPropertyDirectly (VM315 survey.core.js:2271:14)
    at ./src/base.ts.Base.setPropertyValue (VM315 survey.core.js:2258:22)
    at SurveyModel.set (VM315 survey.core.js:60368:18)
    at SurveyComponent (<anonymous>:5:27)
    at mf (VM313 react-dom.prod…tion.min.js:105:412)
    at Qk (VM313 react-dom.prod…tion.min.js:250:214)
    at Ri (VM313 react-dom.prod…tion.min.js:195:185)
    at Ok (VM313 react-dom.prod…tion.min.js:195:116)
JaneSjs commented 2 months ago

The exception occurs becase an object passed to the calculatedValues array is not a valid calculated value. To solve the issue, create an instance of a CalculatedValue object.

import { CalculatedValue } from "survey-core";

survey.calculatedValues.push(new CalculatedValue({ name: "Foo", expression: "bar" }));