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

2

6

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

Claribelclarice answered 1/1, 2023 at 21:45 Comment(0)
C
9

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

Claribelclarice answered 2/1, 2023 at 9:19 Comment(2)
Marking commands and small snippets as "Code Sample" would make the answer easier to visually parse. Ex: Use command cd /home/user.Overdo
Thanks, mate, this resolved my issue with the "Error: URL.search is not implemented" error msg.Miguelinamiguelita
J
7

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 :)

Jackscrew answered 30/3, 2023 at 17:5 Comment(1)
I had to manually add import 'react-native-url-polyfill/auto'; at the top of index.js; and then it worked!Guarantor

© 2022 - 2024 — McMap. All rights reserved.