question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using a Brace mode causes Uncaught Type Error

See original GitHub issue

Problem

I’ve been getting some mysterious errors in the “uncontrolled” mode of my Code Editing component built with AceEditor. It happens in an anonymous function and doesn’t provide much information. I’ve narrowed it down to see that it occurs when I pass in a mode prop to the Ace Editor. But I can’t decipher it further than that.

My dependencies:

"brace": "0.11.1",
"react": "16.4.2",
"react-ace": "6.3.2",

The exact text of the error is this:

index.js:18058 {message: "Uncaught TypeError: Cannot read property 'substring' of undefined", data: undefined, file: "blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7", line: 1, col: 9574, …}col: 9574data: undefinedfile: "blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7"line: 1message: "Uncaught TypeError: Cannot read property 'substring' of undefined"stack: "TypeError: Cannot read property 'substring' of undefined↵    at exports.applyDelta (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1:9574)↵    at Document.applyDelta (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1:21858)↵    at Array.<anonymous> (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1:26690)↵    at Sender.EventEmitter._signal (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1:10664)↵    at window.onmessage (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1:3542)"__proto__: Object
reportError @ index.js:18058
onMessage @ index.js:18049
16:05:05.710 blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1 Uncaught TypeError: Cannot read property 'substring' of undefined
    at exports.applyDelta (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1)
    at Document.applyDelta (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1)
    at Array.<anonymous> (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1)
    at Sender.EventEmitter._signal (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1)
    at window.onmessage (blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1)
exports.applyDelta @ blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1
applyDelta @ blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1
(anonymous) @ blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1
EventEmitter._signal @ blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1
window.onmessage @ blob:http://localhost:8070/e57cc920-a648-4d98-b50a-056519dcb0b7:1

Here’s a repro case:

Component:

import React, { Component } from 'react';
import AceEditor from 'react-ace';
import 'brace/mode/json';

export default class CodeEditor2 extends Component {
	constructor(props) {
		super(props);
		this.state = {
			codeValue: props.value || '',
		};
		this.updateValue = this.updateValue.bind(this);
	}

	updateValue(value) {
		this.setState({
			codeValue: value,
		});
		this.props.onChange && this.props.onChange(value);
	}

	render() {
		const { value, onChange, ...aceProps } = this.props;

		return (
			<div>
				<AceEditor
					onChange={this.updateValue}
					value={value || this.state.codeValue}
					{...aceProps}
				/>
			</div>
		);
	}
}

Container:

import React, { Component } from 'react';
import CodeEditor2 from "../../../src/components/code_editor/code_editor_2";

export default class Sandbox extends Component {
	constructor(props) {
		super(props);
		this.state = {
			codeValue: '{}',
		};
		this.onCodeChange = this.onCodeChange.bind(this);
	}

	onCodeChange(value) {
		this.setState({
			codeValue: value,
		});
	}

	render() {
		return (
			<div>
				<CodeEditor2
					onChange={this.onCodeChange}
					value={this.state.codeValue}
					mode="json"
				/>
			</div>
		);
	}
}

I’ve spent hours trying to figure out what I’m doing wrong, and just can’t figure it out. Is this a bug?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
PrimeObjectscommented, Jun 8, 2019

Any fix to it?

0reactions
livykcommented, May 6, 2022

I found the way how to fix similar problem with react-hook-form. The main problem is the react-hook-form state (or any external state) and AceEditor state (internal state) became out of sync after onChange. When onChange happened, AceState already have new value, but external state (value prop) can’t update for a few renders and still equal to previous value. This is cause of the error. So that’s how I fixed it.

export function AceAdapter({ field: { value, onChange } }) {
  const [lastStringValue, setLastStringValue] = useState(value)

  const handleChange = useCallback((newValue) => {
    setLastStringValue(newValue)
    onChange(newValue)
  }, [onChange])

  useEffect(() => {
    setLastStringValue(value)
  }, [value])

   return (
      <AceEditor
         value={lastStringValue}
         onChange={handleChange}
       />
    )
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: ace.acequire is not a function
I do install brace, but it is not working. I tried giving dependencies file in scripts in angular.json, but it's not working. Here's...
Read more >
SyntaxError: missing } after function body - JavaScript | MDN
The JavaScript exception "missing } after function body" occurs when there is a syntax mistake when creating a function somewhere. Check if any...
Read more >
A Definitive Guide to Handling Errors in JavaScript - Kinsta
The InternalError type is used when an exception occurs in the JavaScript runtime engine. It may or may not indicate an issue with...
Read more >
Uncaught TypeError : Cannot read properties of undefined
The error clearly says that it is Undefined, which means the variable might be declared or used. Still, there is no value associated...
Read more >
Backward incompatible changes - Manual - PHP
This means that custom error handlers may no longer be triggered because exceptions may be thrown instead (causing new fatal errors for uncaught...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found