Ahmed0h
Ahmed0h

Reputation: 323

Expo react-native Error: URL.search is not implemented when working with openai.createImage General API discussion

this is my function

async function generateImage() {
    try {
      const response = await openai.createImage({
        prompt: 'a white siamese cat',
        n: 1,
        size: '1024x1024',
      });
      console.log(response);
    } catch (error) {
      console.error(error);
    }
  }

i am getting this error when using on mobile : Error: URL.search is not implemented

i am really stuck in this one so any help is appreciated

Upvotes: 6

Views: 6069

Answers (2)

Boopy
Boopy

Reputation: 573

You should use react-native-url-polyfill. It's a homemade polyfill to implement the URL API.

$ yarn add react-native-url-polyfill

or

$ npm install react-native-url-polyfill --save

At the top of your entry-point file (index.js), the polyfill will be automatically applied.

/**
 * @format
 */

import "react-native-url-polyfill/auto"
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);


Then just use the url api in any files without any import

    const _url = new URL(url)
    const params = _url.searchParams
    const routeName = _url.hostname
    ...

I hope it will help :)

Upvotes: 9

Ahmed0h
Ahmed0h

Reputation: 323

i found the solution you have to install npm install react-native-url-polyfill and import "react-native-url-polyfill/auto" in your App.js

Upvotes: 9

Related Questions