最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fuse.js constructor issue - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 8

You'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'

发布评论

评论列表(0)

  1. 暂无评论