Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FTS fails (any everything crashes with it) if value of the field is not present #180

Closed
gotenxds opened this issue Aug 22, 2020 · 1 comment

Comments

@gotenxds
Copy link

gotenxds commented Aug 22, 2020

[X ] Bug report

Hya again ^^

When adding an fts field if the field is optional in the collection, fts will fail on insert, this will cause the insert to fail and stop at the last item inserted.

for example, let's say I have an Item collection and I want to be able to search in the field group but this field is optional, i.e sometimes it has a value, sometimes null

When fts tokenizes the field it will try to split it by whitespaces

and fails, as null.split is not a function

Edit:
After testing for 1 more minute 🤦
I found the offending line:

if (doc[fieldName] !== undefined) {

Changing this to

if (doc[fieldName] !== undefined && doc[fieldName] !== null) { ...

fixes the issue

Expected behavior

If a field value is not present, that is null or undefined, skip.
Maybe even check for more types and throw/convert to a string?
Number => toString ? throw invalid type error? expected string got X

Minimal reproduction of the problem with instructions

Something like this should work:

//Initialize DB
    FullTextSearch.register()
    IndexedStorage.register()
    const lokiDb = new Loki('notAMimic')

// initialize collection
   db.addCollection<T>(collectionName, {
   fullTextSearch: [
        { field: 'name' },
        { field: 'group' },
        { field: 'category' },
      ],
  })

// add data
collection.value.insert([
{ name: 'Sword of madness', group: 'sword', category: 'Martial' },
{ name: 'Club', group:'club', category: 'simple' },
{ name: 'Shield of awesome', group: null, category: 'to lazy to come up with something' } // Cry here
])
@Viatorus
Copy link
Member

Have a look. #181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants