Javascript TypeError $(…).functionName is not a function

白昼怎懂夜的黑 提交于 2020-01-17 01:41:12

问题


I'm using the following Javascript to get data via PHP (upload.php) -

<script src="js/jquery.amsify.suggestags.js"></script>

<script>
fetch('./get_hashtags.php').then(response => {
  return response.json();
}).then(data => {        
    var suggestionsArr = [];

    data.forEach(function(item){ 
        suggestionsArr.push(item.category);
    });

    console.log(suggestionsArr);

    $('#inputTags').amsifySuggestags({
        suggestions: suggestionsArr,
        classes: ['bg-warning','bg-warning'],
    });
}).catch(err => {
  console.log(err);
});
</script>

get_hashtags.php

<?php
// ... some additional database connection details ...
$statement = $pdo->prepare("SELECT category FROM hashtags where active=1");

$hashtags = array();
if ($statement->execute()) {
    while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
        $hashtags[] = $row;
    }
}

$pdo = null;

echo json_encode($hashtags);
?>

Unfortunately I'm getting the following error when loading the page -

TypeError: $(...).amsifySuggestags is not a function
at fetch.then.then.data (upload.php:1550)

This is the beginning of jquery.amsify.suggestags.js (https://github.com/amsify42/jquery.amsify.suggestags) -

(function($) {

$.fn.amsifySuggestags = function(options, method) {
    /**
     * Merging default settings with custom
     * @type {object}
     */
    var settings = $.extend({
        type          : 'bootstrap',
        tagLimit      : 5,
        suggestions   : [],
        classes       : [],
        backgrounds   : [],
        colors        : [],
        whiteList     : false,
        afterAdd      : {},
        afterRemove   : {},
    }, options);

    /**
     * Initialization begins from here
     * @type {Object}
     */
    var AmsifySuggestags = function() {
        this.selector      = null;
        this.name          = null;
...

I don't get the error message when inserting the $('#inputTags').amsifySuggestags outside of the fetch method with some hardcoded values.

Thanks in advance!

来源:https://stackoverflow.com/questions/52818048/javascript-typeerror-functionname-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!