wordpress-api-client icon indicating copy to clipboard operation
wordpress-api-client copied to clipboard

TypeError: url_1.URLSearchParams is not a constructor

Open ThijsOnGitHub opened this issue 2 years ago • 6 comments

Describe the bug When I run this code i got this error:

Code run:

const wordpressClient = new WordpressClient();
 const posts = await wordpressClient.post().find()

With the following class, and wordpress is running on localhost:80

export class WordpressClient extends WpApiClient{
    constructor() {
        super("http://localhost");
    }

    post<P = WPPost<WordpressPost>>(): DefaultEndpointWithRevision<P> {
        return super.post<P>()
    }

}
Uncaught (in promise) TypeError: url_1.URLSearchParams is not a constructor
    at WordpressClient.createEndpointGet (wp-api-client.js:40:48)
    at WordpressClient.defaultEndpoints (wp-api-client.js:103:24)
    at WordpressClient.addPostType (wp-api-client.js:112:21)
    at WordpressClient.post (wp-api-client.js:181:21)
    at WordpressClient.post (WorpressClient.ts:10:22)
    at loadPosts (useWordpressData.ts:15:45)
    at useWordpressData.ts:44:9
    at commitHookEffectListMount (react-dom.development.js:23150:26)
    at commitPassiveMountOnFiber (react-dom.development.js:24926:13)
    at commitPassiveMountEffects_complete (react-dom.development.js:24891:9)

To Reproduce Steps to reproduce the behavior:

  1. Run this code:
 const posts = await wordpressClient.post().find()

Expected behavior Expected to load data

Desktop (please complete the following information):

  • Browser: Chrome

Builder:

  • React build with vite

Suggested change I got it locally fixed by removing

import { URLSearchParams } from 'url'

ThijsOnGitHub avatar Feb 07 '23 13:02 ThijsOnGitHub

I got it locally fixed by removing

import { URLSearchParams } from 'url'

hey, thanks for reaching out. what NodeJS version are you using? i'll have to investigate this a bit more, but it seems like an easy fix. sadly i can't give you a time frame on the update, because i am currently undergoing a very strenuous medical treatment.

dkress59 avatar Feb 08 '23 08:02 dkress59

I use node version 16.13.0. And good luck with your medical treatment.

ThijsOnGitHub avatar Feb 10 '23 19:02 ThijsOnGitHub

Hm, I have no issues with Node.js v16. Are you by any chance pulling in the "url" module in your webpack config?

dkress59 avatar Feb 11 '23 07:02 dkress59

Hi I'm having the same issue too - I'm using parcel.js rather than webpack and it is adding the "url" package to the package.json

lorna-propelle avatar Mar 30 '23 12:03 lorna-propelle

same issue, node v20.2.0, Vite + Vue3

firecrauter avatar Jun 12 '23 01:06 firecrauter

same issue, node v20.2.0, Vite + Vue3

Please see the other open issue - the author passed away.

Goldziher avatar Jun 12 '23 05:06 Goldziher