rut_0_1
rut_0_1

Reputation: 761

Uncaught TypeError: $(...).easyAutocomplete is not a function

I am creating a web app using Node js and express. For autocomplete I am using a third party node module called easy-autcomplete. I have followed the documentation and included all the files however I am getting the following error

Uncaught TypeError: $(...).easyAutocomplete is not a function
    at HTMLDocument.<anonymous> (post-property.js:289)
    at mightThrow (jquery-3.3.1.js:3534)
    at process (jquery-3.3.1.js:3602)

post-property.js

$(function(){

    let countries = [
        {"name": "Afghanistan", "code": "AF"},
        {"name": "Albania", "code": "AL"},
        {"name": "Algeria", "code": "DZ"},
..
..
    ]
    var options = {
        data: countries,

        getValue: "name",

        list: {
            match: {
                enabled: true
            }
        }
    };
    $("#provider-json").easyAutocomplete(options);

});

post-property.hbs

<div class="row">
    ..
    ..
</div>
<script src="/javascripts/post-property.js"></script>

<script src="/scripts/jquery.easy-autocomplete.min.js"></script>
<link rel="stylesheet" href="/scripts/easy-autocomplete.css">
<link rel="stylesheet" href="/scripts/easy-autocomplete.themes.min.css">

app.js

...
app.use('/scripts', express.static(__dirname + '/node_modules/easy-autocomplete/dist/'));
...

I checked for various solutions where I had to move my file from the node modules folder however the error persisted.

Upvotes: 0

Views: 4224

Answers (2)

Akim Benchiha
Akim Benchiha

Reputation: 150

You should move the script and css for easy-autocomplete before your own script.

let countries = [
    {"name": "Afghanistan", "code": "AF"},
    {"name": "Albania", "code": "AL"},
    {"name": "Algeria", "code": "DZ"},
]
var options = {
    data: countries,

    getValue: "name",

    list: {
        match: {
            enabled: true
        }
    }
};
$("#basics").easyAutocomplete(options);
<head>
  <link rel="stylesheet" href="/scripts/easy-autocomplete.css">
  <link rel="stylesheet" href="/scripts/easy-autocomplete.themes.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/jquery.easy-autocomplete.min.js"></script>
  <script src="/javascripts/post-property.js"></script>
</head>
<body>
  <div class="row">
      <input id="basics" />
  </div> 
</body>

Then don't forget to include jQuery script as mentionned in the documentation

Upvotes: 3

steadweb
steadweb

Reputation: 16601

You need to load the plugin / library before your custom code, otherwise it won't exist. jQuery also needs to be included before both of these scripts.

Try the following:

<script src="/scripts/jquery.easy-autocomplete.min.js"></script>
<script src="/javascripts/post-property.js"></script>

Upvotes: 1

Related Questions