Expo react-native 错误:使用 openai.createImage 时未实现 URL.search 一般 API 讨论

Ahm*_*d0h 5 react-native openai-api

这是我的功能

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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

我在移动设备上使用时收到此错误:错误:URL.search 未实现

我真的陷入了困境,所以非常感谢任何帮助

Ahm*_*d0h 7

我找到了您必须安装 npm install react-native-url-polyfill 并在 App.js 中导入“react-native-url-polyfill/auto”的解决方案

  • 将命令和小片段标记为“代码示例”将使答案更易于直观地解析。例如:使用命令“cd /home/user”。 (3认同)

小智 7

您应该使用react-native-url-polyfill。这是一个自制的polyfill实现URL API 的方法。

$ yarn add react-native-url-polyfill
Run Code Online (Sandbox Code Playgroud)

或者

$ npm install react-native-url-polyfill --save
Run Code Online (Sandbox Code Playgroud)

在入口点文件的顶部(index.js),polyfill 将自动应用。

/**
 * @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);

Run Code Online (Sandbox Code Playgroud)

然后只需在任何文件中使用 url api,无需任何import

    const _url = new URL(url)
    const params = _url.searchParams
    const routeName = _url.hostname
    ...
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助:)