Eranga Kapukotuwa
Eranga Kapukotuwa

Reputation: 4962

draft-js convertToRaw method is not working

I use draft-js-plugins. I used the emoji and the mention plugins together in my Editor. When I am using ,

convertToRaw(contentState)

to save my content data into the db, it shows an error message.

immutable.js:1421 Uncaught TypeError: Cannot read property 'entries' of undefined

But this error only comes when I have used mention in my content. it works fine with plain text and emojis contents. Please help.

Upvotes: 1

Views: 2666

Answers (2)

Eranga Kapukotuwa
Eranga Kapukotuwa

Reputation: 4962

After working with this for a several time I found the reason. Here I am trying to save an immutable content into the DB. And trying to pass that data to the back-end via an Ajax call.

When we post data via an ajax call, we have to use JSON.stringify and application/json; charset=utf-8 as follows.

 $.ajax({
        url: '/calendar/event/add',
        method: "POST",
        dataType: "JSON",
        data: JSON.stringify(postData),
        contentType: "application/json; charset=utf-8",
    }).done(function (data, text) {
        if(data.status.code == 200){
            console.log("Success");
        }
    }.bind(this));

Upvotes: 0

kaushik94
kaushik94

Reputation: 687

Are you using draft-js plugins editor? It works fine for me, although I got confused between editorState and contentState in the beginning. These kind of errors pop up when I try to do convertToRaw(editorState) make sure you are doing something like this:

_onChange (editorState) {
  this.setState({
    editorState,
  });
  const contentState = editorState.getCurrentContent();
  console.log(convertToRaw(contentState));
}

Upvotes: 7

Related Questions