简体   繁体   中英

Uncaught TypeError: undefined is not a function

I have a script that pulls json data and converts it to a readable format.

It is returning the error: Uncaught TypeError: undefined is not a function on the line container.append($.nano(template, events[0]));

I have tried to find answers on stackoverflow which include:

  • ensure jquery is loaded first
  • wrap function in a document ready
  • include jquery only once

I have done all of the above and have replicated the problem in this jsfiddle

http://jsfiddle.net/FXcX3/

You're calling nano incorrectly, it's not a method of jQuery. It should be like this (from the documentation ):

container.append(nano(nogig));

http://jsfiddle.net/4pe3j/1/

nano is on line 244 in the fiddle. I'm not sure where I'm not defining it.

On that line, you have this:

function nano(template, data) {
    return template.replace(/\{([\w\.]*)\}/g, function (str, key) {
        var keys = key.split("."),
            v = data[keys.shift()];
        for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
        return (typeof v !== "undefined" && v !== null) ? v : "";
    });
}

This is defined on the global scope, so change $.nano to nano .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM