#community-help

Resolving 'typesense is not defined' Error

TLDR Tim couldn't run Typesense due to a missing import statement. Masahiro and Jason helped identify the issue and adjusted the documentation accordingly.

Powered by Struct AI

1

Apr 05, 2021 (34 months ago)
Tim
Photo of md5-95d92c145889874fc74216eaa013aff9
Tim
06:54 AM
I'm getting "typesense is not defined" whenever I try to run... I followed everything in the Install/configure guide...
Masahiro
Photo of md5-366dff6b5f9b1a7d0f404fdc3261e573
Masahiro
06:57 AM
Could you paste your codes?
Tim
Photo of md5-95d92c145889874fc74216eaa013aff9
Tim
07:00 AM
var fs = require('fs');
var readline = require('readline');

let client = new Typesense.Client({
    'nodes': [{
      'host': 'localhost',
      'port': '8108',
      'protocol': 'http'
    }],
    'apiKey': '<API_KEY>',
    'connectionTimeoutSeconds': 2
  })


let booksSchema = {
    'name': 'books',
    'fields': [
        {'name': 'title', 'type': 'string' },
        {'name': 'authors', 'type': 'string[]' },
        {'name': 'image_url', 'type': 'string' },

        {'name': 'publication_year', 'type': 'int32' },
        {'name': 'ratings_count', 'type': 'int32' },
        {'name': 'average_rating', 'type': 'float' },

        {'name': 'authors_facet', 'type': 'string[]', 'facet': true },
        {'name': 'publication_year_facet', 'type': 'string', 'facet': true },
    ],
    'default_sorting_field': 'ratings_count'
}



client.collections().create(booksSchema)
  .then(function (data) {
    console.log(data)
  })


readline.createInterface({
    input: fs.createReadStream('./books.jsonl'),
    terminal: false
}).on('line', function(line) {
   let bookDocument = JSON.parse(line);
   client.collections('books').documents().create(bookDocument)
});

let searchParameters = {
    'q'         : 'harry',
    'query_by'  : 'title',
    'sort_by'   : 'ratings_count:desc'
  }
  
  client.collections('books')
    .documents()
    .search(searchParameters)
    .then(function (searchResults) {
      console.log(searchResults)
    })
07:00
Tim
07:00 AM
I literally used the example code
Masahiro
Photo of md5-366dff6b5f9b1a7d0f404fdc3261e573
Masahiro
07:04 AM
Did you run npm i in your working director?
Tim
Photo of md5-95d92c145889874fc74216eaa013aff9
Tim
07:06 AM
yes
07:06
Tim
07:06 AM
i did npm install --save typesense
Masahiro
Photo of md5-366dff6b5f9b1a7d0f404fdc3261e573
Masahiro
07:52 AM
where is your typesense import?
import Typesense from 'typesense'
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:28 PM
Tim Echoing what Masahiro said, you want to add either an import statement or require statement to get that block of code to work.
10:29
Jason
10:29 PM
Looks like that line of code is missing in the snippet in the docs, I'll add that in shortly
10:48
Jason
10:48 PM
I've added it now

1