Viima / jquery-comments

The Javascript library of choice for implementing commenting in your web app
http://viima.github.io/jquery-comments/
MIT License
294 stars 118 forks source link

TypeError: inputText is undefined #97

Closed bluesun3k1 closed 7 years ago

bluesun3k1 commented 7 years ago

Hello,

I saw a post with the same tittle but with no response and I don't think the issue is based on the same as mine, so I decided to open a new issue.

I’m trying out this plugin for the first time and trying to make it fit my needs. So far everything has turned out well when fetching comments from the database but when posting comments, I keep getting the following error. “TypeError: inputText is undefined” and it points to the following line.

“return inputText.replace(new RegExp('\u00a0', 'g'), ' '); // Convert non-breaking spaces to regular spaces”.

One important note is that the comments ARE saved to the database. I get the error after the query has finished and the comments do not show up until I refresh the page.

I am not sure if this helps but I am not using the jquery-comments.min.js file but the jquery-comments.js file so I can have a better scope of everything that’s in the plugin.

This is what I’m using to post the comments.

      postComment: function(commentJSON, success, error) {
           $.ajax({
            type: 'post',
            url: 'comments.php',
            data: commentJSON,
            success: function(comment) {
                          success(comment)
                   },
                   error: error
                 });
            },

Here’s a sample of the server-side code (comments.php) to insert into the database. I am only taking the “content” from the POST and only using 3 parameters to save data to the db.

                $creator_id = $user_id; //this is predefined from the user’s session ID.
                $content = $_POST['content'];

                $sql = "INSERT INTO comments
                (creator_id, date_created, content)
                VALUES
                ('$creator_id', NOW(), '$content')";
                $res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));

Not sure what the issue could be.

Thanks in advance,

bluesun3k1 commented 7 years ago

I also tried with the minimized file and now I get "TypeError: a not defined" and points to "return a.replace(" ", " ")" which is pretty much the same error as before.

adanski commented 7 years ago

AJAX success callback of POST method expects data as a response (see here), in case of this script you should just respond with the data that was inserted into DB.

bluesun3k1 commented 7 years ago

Thank you. I won't be able to verify this until Monday but once I test, I will post back my results.

bluesun3k1 commented 7 years ago

@adanski. I got it to work.

I took some time to better understand how the script works and I found that when a comment is submitted, the commentJSON function creates a structured comment and then POST the data that will be sent to the server while creating and appending a new "temporary" comment.

All I had to do was to call the commentJSON on the success response so it displays the "temporary comment" (don't know what else to call it) while the actual data is saved in the database and retrieved when the page is loaded again.

postComment: function(commentJSON, success, error) { $.ajax({ type: 'post', url: 'comments.php', data: commentJSON, success: function() { success(saveComment(commentJSON)); }, error: error }); },

This worked for me and allowed me to setup the edit, reply and delete functions successfully.

adanski commented 7 years ago

That's an interesting approach too. Thanks for sharing!

PS. If you're working with JSON, don't forget to set contentType to 'application/json; charset=UTF-8' and also JSON.stringify() the commentJSON before sending: data: JSON.stringify(commentJSON),.