typesense / typesense-js

JavaScript / TypeScript client for Typesense
https://typesense.org/docs/api
Apache License 2.0
414 stars 75 forks source link

client.collections(...).documents is not a function #158

Closed blue-eyed-devil closed 11 months ago

blue-eyed-devil commented 1 year ago

Description

I am trying to index a new document but I keep getting the JS error

Steps to reproduce

const typesenseClient = new Typesense.Client({
  'nodes': [{
    'host': 'search.mydomain.com', 
    'port': 443,
    'protocol': 'https'
  }],
  'apiKey': 'API KEY',
  'connectionTimeoutSeconds': 2
})

await typesenseClient.collections("authors").documents().create({name: "Some Name"})

Expected Behavior

Insert new document

Actual Behavior

Uncaught (in promise) TypeError: typesenseClient.collections(...).documents is not a function

Metadata

Typesense Version:

OS:

jasonbosco commented 1 year ago

It sounds like you might be using an instance of SearchClient instead of Client. Only the latter supports document creation.

blue-eyed-devil commented 1 year ago

With same client instance I am able to create/delete collection or list collections.

const res = await typesenseClient.collections().create(newCollection as CollectionCreateSchema)

or

const res = await typesenseClient.collections(name).delete()

or

const collections = await typesenseClient.collections().retrieve()
jasonbosco commented 1 year ago

Hmmm, here's a standalone example where this works: https://github.com/typesense/typesense-js/blob/master/doc/examples/server/documents.js

Could you try replicating the issue using that snippet and then share the full script?

blue-eyed-devil commented 1 year ago

the very first thing I noticed is the

require('@babel/register')

what I am trying to achieve is to make a browser based utility(for localhost) to manage collections and documents.

is this something that can be causing the issue?

blue-eyed-devil commented 1 year ago

This is the form:

Screen Shot 2023-04-20 at 2 47 01 pm

This is the function:

Screen Shot 2023-04-20 at 2 48 24 pm

This is the result

Screen Shot 2023-04-20 at 2 47 24 pm
blue-eyed-devil commented 1 year ago

I sorted it out. The problem was that i was sending undefined collection name. I just realized that I was referencing newRecord as document and then removing collection property. which made newRecord.collection undefined. BTW example you provided run without a single issue.

Thanks for your time