duffelhq / duffel-api-javascript

JavaScript client library for the Duffel API

Home Page:https://duffel.com/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

suggestions function not working

abiodunt97 opened this issue · comments

Referencing the documentation provided here: https://duffel.com/docs/api/places/get-place-suggestions

I implemented a quick function to test the suggestions function.

Duffel Component

var { Duffel } = require('@duffel/api');

const duffel = new Duffel({
  token: "duffel_test_XXX",
  debug: { verbose: true }
});

module.exports = duffel;

Router Component

var express = require("express");
var router = express.Router();
var duffel = require('../duffel');

router.get("/", async (req, res, next) => {
    const autosuggestion = await duffel.suggestions.list(
        {
        "query": "heathr"
      }
      )
    res.send(autosuggestion);
});

module.exports = router;

Error Message

TypeError: Cannot read properties of undefined (reading 'list')

It seems as though the suggestions function does not exist.

Please help.

Hi @abiodunt97, thanks for reporting the issue! I see we documented how to integrate place suggestions endpoint, but unfortunately, we still didn't do the implementation on the SDK! I have raised the above to our team and will get back to you once it's implemented.

@abiodunt97 we just add the method on our latest version! Let me know if we can help further!

Thanks @lapa182, i've tried to implement it but i'm running into more issues.

Here is the error I get:

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\xxx\Desktop\xxx\app\xxx\server\node_modules\node-fetch\src\index.js from C:\Users\xxx\Desktop\xxx\app\xxx\server\node_modules\@duffel\api\dist\index.js not supported.
Instead change the require of C:\Users\xxx\Desktop\xxx\app\xxx\server\node_modules\node-fetch\src\index.js in C:\Users\xxx\Desktop\xxx\app\xxx\server\node_modules\@duffel\api\dist\index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\Users\xxx\Desktop\xxx\app\xxx\server\node_modules\@duffel\api\dist\index.js:1:75)
    at Object.<anonymous> (C:\Users\xxx\Desktop\xxx\app\xxx\server\src\routes\index.js:3:23)
    at Object.<anonymous> (C:\Users\xxx\Desktop\xxx\app\xxx\server\src\app.js:7:19)
    at Object.<anonymous> (C:\Users\xxx\Desktop\xxx\app\xxx\server\bin\www:31:11) {
  code: 'ERR_REQUIRE_ESM'
}

@lapa182

I managed to resolve the above by going into the index.js and changing the require('node-fetch') to import('node-fetch'), not sure why this version throws aan ERR_REQUIRE_ESM error.

Also, in this new version 1.13.0. I am getting the below error:

TypeError: i.default is not a function
    at $.<anonymous> (C:\Users\xxx\Desktop\xxx\xxx\xxx\server\api\node_modules\@duffel\api\dist\index.js:1:7334) 
    at Generator.next (<anonymous>)
    at C:\Users\xxx\Desktop\xxx\xxx\xxx\server\api\node_modules\@duffel\api\dist\index.js:1:809
    at new Promise (<anonymous>)
    at h (C:\Users\xxx\Desktop\xxx\xxx\xxx\server\api\node_modules\@duffel\api\dist\index.js:1:554)
    at $.request (C:\Users\xxx\Desktop\xxx\xxx\xxx\server\api\node_modules\@duffel\api\dist\index.js:1:6452)     
    at T.<anonymous> (C:\Users\xxx\Desktop\xxx\xxx\xxx\server\api\node_modules\@duffel\api\dist\index.js:1:1775) 
    at Generator.next (<anonymous>)
    at C:\Users\xxx\Desktop\xxx\xxx\xxx\server\api\node_modules\@duffel\api\dist\index.js:1:809
    at new Promise (<anonymous>)

Please help.

@lapa182 Any news on this would be much appreciated, thanks.

Hi @abiodunt97, sorry I was off and wasn't able to help you before! Let me investigate the issue and will come back to you.

Regarding node-fetch, I have a feeling that's the version we use which is the newest node-fetch version which is built to be used as ESM.

Instead of manually updating to import('node-fetch') you can resolve the module and install the previous v2 version https://github.com/node-fetch/node-fetch#installation. Alternatively, you can compile the module on the framework settings you are using - Next.js for example allows you to transpile modules manually.

Hey @abiodunt97, I just investigate the issue and I think it's the issue it's caused by the change you made to node-fetch.

Could you please add to your package.json the following line:

If you are using npm:

"overrides": {
    "node-fetch": "2.6.7"
  },

If you are using yarn:

"resolutions": {
    "node-fetch": "2.6.7"
  },

And them re-install your packages.

No luck, still getting the same error with the overrides option added,

No luck, still getting the same error with the overrides option added,

Do you mean the ESM error or .default one? Also, could you say what node version are you using?

@abiodunt97 we deployed a new version of the library, could you please check if the issue still persists? Could you try deleting your node_modules and reinstalling the packages?

A huge thank you @lapa182. It now works for me perfectly. Thanks for all your help.