olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.91k stars 547 forks source link

`add` seems that it is worked is different from version 2? #254

Closed Vonfry closed 7 years ago

Vonfry commented 7 years ago

add seems that it is worked is different from version 2?

The same code and json data can be run correctly in version 1.0.0, but it cannot be run in version 2.0.0. And I cannot find any question in my code.

expection is here:

TypeError: Cannot read property '1' of undefined
    at e.Builder.add (cdnjs.cloudflare.com/ajax/libs/lunr.js/2.0.0/lunr.min.js:6)
    at e.Builder.<anonymous> (:4000/assets/js/search.js:49)
    at e (cdnjs.cloudflare.com/ajax/libs/lunr.js/2.0.0/lunr.min.js:6)
    at :4000/assets/js/search.js:36
    at _4ec (dojo.js:15)
    at _4e6 (dojo.js:15)
    at _4eb.resolve (dojo.js:15)
    at _4f2 (dojo.js:15)
    at _4ec (dojo.js:15)
    at _4e6 (dojo.js:15) "TypeError: Cannot read property '1' of undefined
    at e.Builder.add (https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.0.0/lunr.min.js:6:14950)
    at e.Builder.<anonymous> (http://localhost:4000/assets/js/search.js:49:30)
    at e (https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.0.0/lunr.min.js:6:145)
    at http://localhost:4000/assets/js/search.js:36:29
    at _4ec (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128307)
    at _4e6 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128171)
    at _4eb.resolve (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:129632)
    at _4f2 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128913)
    at _4ec (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128523)
    at _4e6 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128171)
    ----------------------------------------
    rejected at _4f2 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128948)
    at _4ec (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128559)
    at _4e6 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128171)
    at _4eb.resolve (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:129632)
    at _4f2 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128913)
    at _4ec (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128523)
    at _4e6 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128171)
    at _4eb.resolve (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:129632)
    at _4f2 (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128913)
    at _4ec (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:128588)
    ----------------------------------------
Error
    at Promise.then._4f9.then (http://cdnjs.cloudflare.com/ajax/libs/dojo/1.10.4/dojo.js:15:130120)
    at Object.getSearchJSON (http://localhost:4000/assets/js/search.js:33:16)
    at Object.searchText (http://localhost:4000/assets/js/search.js:62:22)
    at Object.searchfunc (http://localhost:4000/assets/js/search.js:89:18)
    at searchfunc (http://localhost:4000/assets/js/search.js:116:33)
    at HTMLInputElement.<anonymous> (http://localhost:4000/assets/js/search.js:121:21)"

TypeError: Cannot read property '1' of undefined, Un...I don't know what it is.

And part of my code.

        getSearchJSON: function(callback) {
            let self = this;
            let request = require('dojo/request');
            request.get(this.searchJsonUrl, {
                handleAs: "json"
            }).then(function(data) {
                self.searchJSON = data;
                let lunrjs = require('vonfry/lib/lunrjs');
                let index = lunrjs(function() {
                    this.field('title');
                    this.field('content');
                    this.field('link');
                    this.field('date');
                    this.field('excerpt');
                    this.field('categories');
                    this.field('tags');
                    this.field('search_omit');
                    this.ref('id');
                    for (let idx in data) {
                        let _d = data[idx];                    
                        _d['id'] = idx;
                        this.add(_d);
                    }
                });
                self.lunrIdx = index;
                callback.apply(self);
            });
olivernn commented 7 years ago

The document reference must be a string, try this:

_d['id'] = idx.toString()
Vonfry commented 7 years ago

In chrome, idx is string for default. And if I change it to string using toString, it also has the same problem

olivernn commented 7 years ago

Interesting, can you put together a simple test case that I can use to re-create the error?

Vonfry commented 7 years ago

This is an error log in safari undefined is not an object (evaluating 'this.invertedIndex[h][n][t]')

olivernn commented 7 years ago

To be able to help I need to be able to re-create the issue myself. Try and put together an index and document that can be run in something like jsfiddle that also has the same issues.

Vonfry commented 7 years ago

https://test.vonfry.name/lunrjs/

A test case.

olivernn commented 7 years ago

Could you try and narrow it down to just the document that is failing, and narrow that document down to just the field that is causing issues.

Vonfry commented 7 years ago

I try to narrow it down.

The problem seems that it only happen when the content has some special word, but I cannot find which word is the problem...

https://test.vonfry.name/lunrjs https://test.vonfry.name/lunrjs/search.json

Vonfry commented 7 years ago

jsfiddle: https://jsfiddle.net/Vonfry/suu1ju5f/

2017-04-10 21 44 14

Uh...

olivernn commented 7 years ago

Ok, I've update the fiddle with a more minimal reproduction.

This looks like a regression from the 1.0.0 branch. It is because "constructor" is a property of all JavaScript objects, this causes issues when using objects as hashes.

I'll look at putting together a fix for this in the next few hours, thanks for reporting.

olivernn commented 7 years ago

I've just pushed 2.0.1 which includes a fix for this issue, please check it out and let me know if you still have problems...

Vonfry commented 7 years ago

Thank you!! It can be run.

MatthiasRMS commented 7 years ago

I still have an issue with add since I've updated to 2.0.0 (and then 2.0.1). I've simplified it and now I have only this:

var idx = lunr(function () {
  this.field('name')
})

var doc = {
  "name": "hello",
   "id": 1
 }

idx.add(doc)

And in the console:

Uncaught TypeError: idx.add is not a function

This worked perfectly fine before the update. I have this issue in Chrome and Safari (didn't try other browsers)

olivernn commented 7 years ago

@MatthiasRMS this isn't at all related to the original issue, in the future please open a new issue rather than commenting on an unrelated issue.

Also, the error message is pretty clear here, add is not a function on idx. Since there is a major version bump you can expect some breaking changes in the API, this is probably the largest and is well documented.

add must be called within the function passed to the lunr function, i.e.

var idx = lunr(function () {
  this.field('name')
  this.add({ name: "hello", id: "1" })
})
MatthiasRMS commented 7 years ago

@olivernn my bad, I didn't see it in the doc. Sorry about that !