ChiriVulpes / scryfall-sdk

A Node.js SDK for https://scryfall.com/docs/api, written in TypeScript.
MIT License
99 stars 16 forks source link

Runtime error with interfaces exported from Scry module: Cannot read property 'collection' of undefined #28

Closed marcopiii closed 4 years ago

marcopiii commented 4 years ago

I'm getting the following error when I start my React ts app.

TypeError: Cannot read property 'collection' of undefined

   13 | 
   14 | componentDidMount() {
   15 |   const identifiers = cardList.map((cardName) => ({ name: cardName }));
>  16 |   Scry.Cards.collection(...identifiers)
   17 | ^   .waitForAll()
   18 |     .then((collection) => {
   19 |       this.setState({ collection: collection });

The same thing happens with everything I export from Scry, like Scry.CardIdentifier.

I think I'm using it as I saw in the documentation.

version
scryfall-sdk v1.6.3
typescript v3.7.5
node v12.14.1
ChiriVulpes commented 4 years ago

How are you importing the module? Looks like you're not doing it correctly, the only reason why you'd get this problem is if your Scry variable contains something besides scryfall-sdk. IE the error you're getting is identical to if Scry was {}

marcopiii commented 4 years ago

I was importing it as import Scry from "scryfall-sdk" because with the import assignment like in the documentation i was getting Import assignment cannot be used when targeting ECMAScript modules at compile time.

I did some tries and both the following formats work!

import { Cards  } from "scryfall-sdk";
import * as Scry from "scryfall-sdk";`

thank you and sorry for wasting your time, I hope this can be useful to someone else 👍