1

Inside my attemptSearch function, I use jQuery $.post to get some JSON results. I'm getting the error

Undefined is not an object

on the call

this.getSearchResults.bind(this)

I have the same set up in another web app and I don't get this error. What am I doing wrong?

var app = {
    init: function() {
        this.cacheDom();
        this.bindEvents();
    },
    cacheDom: function() {
        this.$search = $('#search');
    },
    bindEvents: function() {
        this.$search.keyup(this.attemptSearch)
    },
    searchResults : [],
    getSearchResults : function(val){       
        var currentSearchResult = val.query.search;
        for (var i = 0; i < currentSearchResult.length; i++) {
            var result = {
                title: currentSearchResult[i].title,
            }
            console.log(this.searchResults);
            this.searchResults.push(result);
        }
    },
    attemptSearch: function(event) {
        var wiki = "https://crossorigin.me/https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=";
        if (event.keyCode == 13) {
            $.post((wiki + $('#search').val()), this.getSearchResults.bind(this))
        }
    }, 
};

app.init();

1 Answer 1

2

You made sure to bind getSearchResults, but you didn't bind attemptSearch. That's almost surely the issue:

this.$search.keyup(this.attemptSearch.bind(this))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you JLRishe, that was indeed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.