steemit / steem-js

Steem.js the official JavaScript library for Steem blockchain
https://www.npmjs.com/package/steem
MIT License
472 stars 225 forks source link

Commenting post Issue #369

Open ghost opened 6 years ago

ghost commented 6 years ago

I found interesting code that works fine to post into steem

<html>
<head><title>steem-js posting example</title></head>
<body>
<script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>
Post Function

<script language="JavaScript">
function postArticle()
{
  steem.broadcast.comment(
    document.getElementById('postingKey').value, // posting wif
    '', // author, leave blank for new post
    'steemtest', // first tag
    document.getElementById('username').value, // username
    'name-of-my-test-article-post', // permlink
    document.getElementById('title').value, // Title
    document.getElementById('article').value, // Body of post
    // json metadata (additional tags, app name, etc)
    { tags: ['secondtag'], app: 'steemjs-test!' },
    function (err, result) {
      if (err)
        alert('Failure! ' + err);
      else
        alert('Success!');
    }
  );
}
</script>
Username: <input id="username" type="text"><br/>
Posting key: <input id="postingKey" type="password" size="65"><br/>
Title of article: <input id="title" type="text"><br/>
Article text:<br/>
<textarea id="article"></textarea><br/>
<input id="postIt" type="button" value="Post it!" onClick=postArticle()>

</body>
</html>

And now I am trying to change that code to make comments for this post. As far as I know but I might be wrong it should something like this:

<html>
<head><title>steem-js posting example</title></head>
<body>
<script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>
Post Function

<script language="JavaScript">
function postArticle()
{
  steem.broadcast.comment(
    document.getElementById('postingKey').value, // posting wif
    document.getElementById('username').value, // author, leave blank for new post
    'steemtest', // first tag
    document.getElementById('username').value, // username
    'name-of-my-test-article-post', // permlink
    '',  // Title
    document.getElementById('article').value, // Body of post
    // json metadata (additional tags, app name, etc)
    { tags: ['secondtag'], app: 'steemjs-test!' },
    function (err, result) {
      if (err)
        alert('Failure! ' + err);
      else
        alert('Success!');
    }
  );
}
</script>

<h2>Trying to post comment</h2>

Username: <input id="username" type="text"><br/>
Posting key: <input id="postingKey" type="password" size="65"><br/>
Title of article: <input id="title" type="text"><br/>
Article text:<br/>
<textarea id="article"></textarea><br/>
<input id="postIt" type="button" value="Post it!" onClick=postArticle()>

</body>
</html>

As far as I did notice you comment posts almost same way as you do create a post. Just remove the title and add the author. But in this case, for some reason, I am getting errors

"RPCError Unknown Key"

Since the author can comment own post I should work with same values. Can anyone know how to fix that problem?

BartolomeoItaliano commented 6 years ago

Hi, I've checked your code two arguments are swapped, if you switch permlink with first tag everything works fine.

Working example:

<html>
<head><title>steem-js posting example</title></head>
<body>
<script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>
Post Function

<script language="JavaScript">
    function postArticle()
    {
        steem.broadcast.comment(
            document.getElementById('postingKey').value, // posting wif
            document.getElementById('username').value, // author, leave blank for new post
            'name-of-my-test-article-post', // permlink
            document.getElementById('username').value, // username
            'steemtest', // first tag
            '',  // Title
            document.getElementById('article').value, // Body of post
            // json metadata (additional tags, app name, etc)
            { tags: ['secondtag'], app: 'steemjs-test!' },
            function (err, result) {
                if (err)
                    alert('Failure! ' + err);
                else
                    alert('Success!');
            }
        );
    }
</script>

<h2>Trying to post comment</h2>

Username: <input id="username" type="text"><br/>
Posting key: <input id="postingKey" type="password" size="65"><br/>
Title of article: <input id="title" type="text"><br/>
Article text:<br/>
<textarea id="article"></textarea><br/>
<input id="postIt" type="button" value="Post it!" onClick=postArticle()>

</body>
</html>
ghost commented 6 years ago

wow, amazing, simple thing and I did spend whole on fixing that. Thank you, thank you a lot