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 5846427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:34:30+00:00 2026-05-22T12:34:30+00:00

What am I doing wrong here? I’m using the fat arrows => for my

  • 0

What am I doing wrong here? I’m using the fat arrows => for my callbacks, however when the code reaches cb.onEndElement and calls @returner I get an null object exception. So why doesn’t @returner exist?

class Parser
    constructor: () ->
        @returner = (data) ->

    searchParser: new xml.SaxParser (cb) =>
        cb.onStartElementNS (elem, attrs, prefix, url, ns) =>
            if elem is "results" then @results = []
            else if elem is "title" then @curr = "title"
            else @curr = "none"
        cb.onCdata (cdata) =>
            if @curr is "title" then @book.title = cdata
        cb.onEndElementNS (elem, prefix, url) =>
            @results.push @book if elem is "book"
        cb.onEndDocument =>
            @returner @results

    search: (str, callback) ->
        @returner = callback
        @searchParser.parseString str

p = new Parser
p.search "somexml", (data) ->
    console.log JSON.stringify data
  • 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-05-22T12:34:31+00:00Added an answer on May 22, 2026 at 12:34 pm

    Your method search needs a fat-arrow => in order to bind it to instances of Parser.

    Additionally, although the line searchParser: new xml.SaxParser (cb) => compiles, it’s probably not doing what you want, because the fat arrow is binding the callback to Parser and not this. You have two options:

    1. you should probably put @searchParser = new xml.SaxParser (cb) => ... in your constructor instead, given the way you are calling it.
    2. otherwise you could use searchParser: () => new xml.SaxParser (cb) => and call it with parens lower down: @searchParser().parseString str, which would create a searchParser method bound to this

    As an example, here are my two solutions, as well as your original line, slightly simplified, as well as the compiled code, for compare and contrast purposes:

    Simplified example in CoffeeScript:

    class Parser
      constructor: () -> @searchParser1 = new xml.SaxParser (x) => console.log(x)
      searchParser2: () => new xml.SaxParser (x) => console.log(x)
      searchParser: new xml.SaxParser (x) => console.log(x)
    

    Compiled JavaScript:

    var Parser;
    var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
    Parser = (function() {
      function Parser() {
        this.searchParser2 = __bind(this.searchParser2, this);    
        this.searchParser1 = new xml.SaxParser(__bind(function(x) {
          return console.log(x);
        }, this));
      }
      Parser.prototype.searchParser2 = function() {
        return new xml.SaxParser(__bind(function(x) {
          return console.log(x);
        }, this));
      };
      Parser.prototype.searchParser = new xml.SaxParser(__bind(function(x) {
        return console.log(x);
      }, Parser));
      return Parser;
    }).call(this);
    

    Note how searchParser1 and searchParser2 have their callbacks bound to this and searchParser‘s is bound to Parser.

    As always, the “Try CoffeeScript” button at the CoffeeScript homepage (http://jashkenas.github.com/coffee-script/) is your friend!

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

Sidebar

Related Questions

What am I doing wrong here? My use of the INTO clause seems to
I can't seem to figure out what I am doing wrong here. I publish
What am I doing wrong? I have a simple console app in VS08. When
I find this very strange, must be something I'm doing wrong, but still... I'm
Am I doing something wrong or is it not possible to specify a generic
I must be doing something wrong. I can't seem to execute my CustomValidator's ServerValidate
Am I doing something wrong is this a known issue with the ASP.NET MVC
I'm sure I must be doing something wrong. But can't for the life of
Just tried to execute a small Lua script, but unfortunately I'm doing something wrong.
I'm probably doing this all wrong. I have a text file full of data

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.