not-an-aardvark / snoowrap

A JavaScript wrapper for the reddit API
MIT License
1.01k stars 125 forks source link

When I send a post I receive a Submission but I have access only to the name #383

Open thedarkknight197 opened 1 year ago

thedarkknight197 commented 1 year ago

Hello everybody, I am using snowcap to publish content on Reddit.

I am using typescript and when I publish any type of content, I receive a submission object with only name. I need to access to all property of object but I can't in any way.

That's my code:

public async  createLinkPost(userId: number, title: string, text: string, subreddit: string, image: string) {
    const r = await this.getClient(userId);

    const data = r.getSubreddit(subreddit);

    await r.submitLink({
      title: title,
      url: text,
      subredditName: data.display_name,
    }).then((s: snoowrap.Submission) => {
      console.log(s); // HERE ONLY: Submission { name: 't3_xxxxxxx' }
      console.log(r.getSubmission(s.name));  // HERE ONLY: Submission { name: 't3_xxxxxxx' }
      post.url = s.url; // empty
    }).catch((error) => console.log(error));
  }

What is the problem?

Thank you tu everybody

pe1uca commented 1 year ago

You need to call s.fetch()

thedarkknight197 commented 1 year ago

Thank you so much I resolved using fetch in this way!

Do you like the then in this way?

public async  createLinkPost(userId: number, title: string, text: string, subreddit: string, image: string) {
    const r = await this.getClient(userId);

    const data = r.getSubreddit(subreddit);

    const post: RedditPost = {
      id: uuidv4(),
      title: title,
      subreddit: data.display_name,
      description: text,
      url: '',
      name: '',
      image,
      createdAt: Date.now(),
    };

    r.submitLink({
      title: title,
      url: text,
      subredditName: data.display_name,
      nsfw: true,
    }).then((s: snoowrap.Submission) => {
      return r.getSubmission(s.name).fetch();
    }).then(submission => {
        console.dir(submission)
        post.name = submission.name;
         post.url = submission.permalink;
         this.redisService.addRedditPost(`${userId}`, post);
    }).catch((error) => console.log(error));
  }