Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:49:18+00:00 2026-06-17T10:49:18+00:00

I am trying to build a reddit api wrapper for node.js. I am fairly

  • 0

I am trying to build a reddit api wrapper for node.js. I am fairly new to OO javascript and I am having an issue with assigning a value to my reddit object’s properties.

var request = require("request");
var subreddit = require("./subreddit").subreddit;

var reddit = function () {

  var self = this,
      userAgent = "node.js api wrapper - https://github.com/theyshookhands/rednode",
      debug = false,
      cookie = "",
      uh = "";  

  self.getJSON = function (url, data, callback) {
    data["api_type"] = "json";
    request(url, { qs: data }, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(body);
      }
    });
  };

  self.post = function (url, data, callback) {
    console.log("in post");
    console.log("cookie: " + self.cookie);
    console.log("uh: " + self.uh);
    data["api_type"] = "json";
    if (self.cookie) {
      request.cookie(self.cookie);
      console.log("cookie assigned");
    }
    if (self.uh) {
      data["uh"] = self.uh;
      console.log("uh assigned");
    }
    console.log("requesting");
    request.post(url, { form: data }, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log("no errors");
        callback(body);
      }
    });
  };

};

reddit.prototype = {

  login: function (username, password, callback) {
    var data = {
      "user": username,
      "passwd": password,
      "rem": false
    };

    this.post("http://www.reddit.com/api/login", data, function (body) {
      var response = JSON.parse(body);
      this.uh = response["json"]["data"]["modhash"];
      this.cookie = response.json.data.cookie;
      console.log("rednode --> logged in as: " + username);
      callback();
    });
  },

  setUserAgent: function (userAgent) {
    this.userAgent = userAgent;
  },

  r: function (name, callback) {
    var sr = new subreddit(name);
    if (callback) {
      sr.exec(callback);
    }
    return sr;
  },

  postLink: function (sr, title, url, callback) {
    var data = {
      "kind": "link",
      "sr": sr,
      "title": title,
      "url": url
    };
    console.log("calling post");
    this.post("http://www.reddit.com/api/submit", data, callback);
  }
}

exports.reddit = reddit;

The objects are defined at lines 15 and 16. They are given values at lines 62 and 63. Why am I not reaching these values using the

this.cookie, this.modhash

syntax?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:49:19+00:00Added an answer on June 17, 2026 at 10:49 am

    In the function:

    var reddit = function () {
    

    Why use a function expression where a function declaration will do and is likely clearer? And by convention, constructors start with a capital letter:

    function Reddit() {
    

    In the function:

      var self = this,
    

    The value of a function’s this is determined entirely by how the function is called. Since this is a constructor, when called with new its this will reference a newly constructed object (an “instance” of the constructor).

      var self = this,
          userAgent = "...",
          debug = false,
          cookie = "",
    

    The above creates variables on the local execution object’s variable object. You can only access variables by name, they are resolved on the scope chain of execution objects, ending with the global object.

    ...
    self.post = function (url, data, callback) {
        console.log("in post");
        console.log("cookie: " + self.cookie);
    

    Here, self on the LHS references the Reddit instance. On the RHS, the function expression has a closure to the outer scope so its self also references the same instance so you are looking for a cookie property on the object and its [[Prototype]] chain.

    But you declared cookie as a local variable of the constructor, so the identifier is being resolved along the wrong chain (i.e. you should be looking for a variable on the scope chain, not a property on the object/[[Prototype]] chain).

    It just so happens that because of the closure, you can use:

        console.log("cookie: " + cookie);
    

    to resolve cookie on the scope chain.

    But it seems like you really want to access it as a property, so just make it a public property in the first place:

      var self = this,
          userAgent = "...",
          debug = false;
    
      this.cookie = "", // or self.cookie = '',
      ...
    

    Now you can use self.cookie.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to build a little bot which will be using Reddit's API.
Trying to build a Metro app using Javascript and having issues with IndexedDb. I
I'm trying build a method which returns the shortest path from one node to
I'm trying build an App Engine connected Android application and am having some problems
Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
Trying to build the OCaml Win32 API binaries for OCaml 3.11.0 on Win 7
Trying to build a basic 3-layer architecture in my new application suite. For first
I'm trying to figure out how to use gson to convert reddit's api response
I am trying to build a Reddit or Hackernews-style community section for my website.
I'm brand new to this and I'm trying build a simple layout of divs.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.