New steem dev series - #2

in #steemit7 years ago

Ok so this one is going to be a bit simplistic but has some issues that I think are interesting. I am going to include my entire class I use to get post data although not the actual code to display the data because that is involved in a way that is specific to whatever ui you are using or error handling. I will also explain the little bit of react being used here.

import React from 'react';
import Steem from 'steem';

class Post extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      postData: null,
      author: this.props.match.params.author,
      permlink: this.props.match.params.permlink
    }
  }
  async populateData() {
    const resultP = await Steem.api.getContentAsync(this.state.author, this.state.permlink);
    this.setState({postData: resultP});
  }
  render() {
    const postData = this.state.postData;
    if(postData===null) {
      return (
        <div>Loading Post</div>
      )
    } else {
      console.log(postData);
      return (
        <div>{JSON.stringify(postData)}
        </div>
      )
    }
  }
  componentDidMount() {
    this.populateData();
  }
}
export default Post;

I am grabbing author and permlink from the url and then calling getContentAsync using those parameters. Now the reason I am making a post about this extremely simple call is because of a bug of mine that exposed what I think is a problem in the api but something you should be aware of so you don't drive yourself crazy trying to figure out what you have done wrong.

Steem.api.getContentAsync(this.state.author, this.state.permLink);
This code seems so simple and innocent but it led to me getting a JSON content object with all empty values in it. The problem was I had a typo and I was calling it with permLink when my variable name was permlink with no camel case. I would expect in this case to get null back and then my code would have thrown an error. Instead it removed the loading post message and replaced it with what looked like a valid response except everything in side of the object was blank or some default value.

I have seen others ask questions assuming their code did not work at all when in reality the code interacting with the api worked fine but some silly little typo like this made them think that they had no clue how to talk to it.

So the moral of the story is be careful with key names and if you get this mess below it means you probably messed up the permlink or author.

{"id":0,"author":"","permlink":"","category":"","parent_author":"","parent_permlink":"","title":"","body":"","json_metadata":"","last_update":"1970-01-01T00:00:00","created":"1970-01-01T00:00:00","active":"1970-01-01T00:00:00","last_payout":"1970-01-01T00:00:00","depth":0,"children":0,"net_rshares":0,"abs_rshares":0,"vote_rshares":0,"children_abs_rshares":0,"cashout_time":"1970-01-01T00:00:00","max_cashout_time":"1970-01-01T00:00:00","total_vote_weight":0,"reward_weight":0,"total_payout_value":"0.000 STEEM","curator_payout_value":"0.000 STEEM","author_rewards":0,"net_votes":0,"root_comment":0,"max_accepted_payout":"0.000 STEEM","percent_steem_dollars":0,"allow_replies":false,"allow_votes":false,"allow_curation_rewards":false,"beneficiaries":[],"url":"","root_title":"","pending_payout_value":"0.000 STEEM","total_pending_payout_value":"0.000 STEEM","active_votes":[],"replies":[],"author_reputation":0,"promoted":"0.000 SBD","body_length":0,"reblogged_by":[]}

Sort:  

Will be helpful for me. Thanks for posting.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.027
BTC 60063.85
ETH 2313.06
USDT 1.00
SBD 2.46