typesense / typesense-js

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

TypeError: client.collections(...).documents(...).retrieve is not a function #94

Closed joshmossas closed 2 years ago

joshmossas commented 2 years ago

Description

Calling client.collections('some-collection').documents('some-doc-id').retrieve() throws the following error:

UnhandledPromiseRejectionWarning: TypeError: client.collections(...).documents(...).retrieve is not a function

Steps to reproduce

Just call the method like specified in the Typesense docs. My code looks like the following:

export const getDoc = async <T = any>(
    client: Typesense.Client,
    collection: SearchCollection,
    documentId: string
) => client.collections<T>(collection).documents(documentId).retrieve();

Expected Behavior

This method should exist and should get the document associated with the documentId that was passed as outlined in the retrieve a document section of the docs

Actual Behavior

You get a "retrieve() is not a function" type error.

Metadata

OS: Windows 10

jasonbosco commented 2 years ago

@joshmossas Could you double check if the actual client being passed into getDoc is Typesense.Client and not Typesense.SearchClient? The latter only supports documents().search().

joshmossas commented 2 years ago

Yes sir, the client being passed into getDoc is Typesense.Client

joshmossas commented 2 years ago

@jasonbosco I figured out the problem.

I was importing typesense like this:

import Typesense from 'typesense';

const client = new Typesense.Client({
    // stuff here
})

But Typesense has no export default so I needed to import it like so.

import * as Typesense from 'typesense';

const client = new Typesense.Client({
    // stuff here
})

///// or alternatively /////

import { Client } from 'typesense';

const client = new Client({
    // stuff here
});
jasonbosco commented 2 years ago

Ah interesting! This was an unintentional change when we converted to Typescript. Will add a default export to prevent any confusion.

jasonbosco commented 2 years ago

Pushed out v1.1.2 where you should now be able to do:

ddtbuilder commented 5 months ago

I tried

Pushed out v1.1.2 where you should now be able to do:

  • import Typesense from 'typesense' or
  • import { Client, SearchClient } from 'typesense' or
  • import * as Typesense from 'typesense'

I tried all of these and I can't seem to get the import correct. Using rollup i always get this error:

Error: 'Typesense' is not exported by node_modules/typesense/lib/Typesense.js