Im trying to add a fuzzy search library to my project via fuse.js. I include the following lines and I'm getting a constructor error, I tried to re-install fuse but I'm wondering where the error may be.
// TypeError: Fuse is not a constructor
var Fuse = require('fuse');
var options = { // list of options that need to be provided to fuse.js for search to occur
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
"title", // the keys that are searched
"description"
]
};
var fuse = new Fuse(posts, options); // "list" is the item array
var result = fuse.search(searchOptions.keywords); // search is conducted and result should be all matching JSON objects
Im trying to add a fuzzy search library to my project via fuse.js. I include the following lines and I'm getting a constructor error, I tried to re-install fuse but I'm wondering where the error may be.
// TypeError: Fuse is not a constructor
var Fuse = require('fuse');
var options = { // list of options that need to be provided to fuse.js for search to occur
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
"title", // the keys that are searched
"description"
]
};
var fuse = new Fuse(posts, options); // "list" is the item array
var result = fuse.search(searchOptions.keywords); // search is conducted and result should be all matching JSON objects
Share
Improve this question
edited Jun 28, 2020 at 18:55
krisk
7,1171 gold badge19 silver badges30 bronze badges
asked Apr 20, 2017 at 22:43
learner561learner561
1372 silver badges11 bronze badges
2 Answers
Reset to default 8You're confusing the fuse.js module with the fuse module, which is a pletely different project. You can see that this is the case by looking at the "Install" section of the Fuse.js website.
To fix this, run npm install --save fuse.js
and fix line with the require to this:
var Fuse = require('fuse.js');
Are you using typescript?
You need to first install the library: run npm install --save fuse.js
.
Then import the library on top of the file that you are going to use the library:
import * as Fuse from 'fuse.js'