typesense/typesense-js

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

blue-eyed-devil opened this issue · 6 comments

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:

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

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()

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?

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?

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

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