why is apiFetch throwing Unhandled Promise Rejection: TypeError: Object is not a function

Question

I’m trying to diagnose a similar problem to this question, but in that case he wasn’t depending on wp-api-fetch, and … I’m pretty sure I am.

I’m getting the following error:

[Error] Unhandled Promise Rejection: TypeError: Object is not a function. (In 'Object(_wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_5__["apiFetch"])', 'Object' is an instance of Object)

(full backtrace, below)

I should note that I’m new to both the REST API and ESNext/Gutenberg plugin development, so … I may be missing something really obvious, like a comma 🙂

Here’s the code:

import { __ } from 'WPBeginner - WordPress Tutorials/i18n';
import { Fragment } from 'WPBeginner - WordPress Tutorials/element';
import { TextControl } from 'WPBeginner - WordPress Tutorials/components';
import { apiFetch } from 'WPBeginner - WordPress Tutorials/api-fetch';

export default function Edit( props ) {
        const {
                attributes: { cwraggDataSourceType, cwraggDataSource,
                    cwraggLocalFile },
                setAttributes,
        } = props;

        const post_id = wp.data.select("core/editor").getCurrentPostId();

        const onChangeContent = async ( newUrl ) => {
                let localFileName = await apiFetch( {
                    path: '/cwraggb/v1/setremotedatasrc',
                    method: 'POST',
                    data: { 'url': newUrl,
                            'type': 'json',
                            'postId': post_id } } );
                ...
  
        };
        ...
}

I looked at the output of npm run start, and it seems to be including the dependencies in the build:

<?php return array('dependencies' => array('wp-api-fetch', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-polyfill'), 'version' => '566e4b7cb2f100542103b2b0e25aefae');

This is being built, and docker run, on MacOS 10.15.7.

~ % npm --version
6.14.8
~ % wp-env --version
2.1.0

Any ideas what’s causing that error, and/or how I can further diagnose?

Full error message:

[Error] Unhandled Promise Rejection: TypeError: Object is not a function. (In 'Object(_wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_5__["apiFetch"])', 'Object' is an instance of Object)
    dispatchException (wp-polyfill.js:7017)
    invoke (wp-polyfill.js:6738)
    asyncGeneratorStep (cwra-google-graph-block-admin.js:250)
    _next (cwra-google-graph-block-admin.js:272)
    (anonymous function) (cwra-google-graph-block-admin.js:279)
    Promise
    (anonymous function) (cwra-google-graph-block-admin.js:268)
    callCallback (react-dom.js:341)
    dispatchEvent
    invokeGuardedCallbackDev (react-dom.js:391)
    invokeGuardedCallback (react-dom.js:448)
    invokeGuardedCallbackAndCatchFirstError (react-dom.js:462)
    executeDispatch (react-dom.js:594)
    executeDispatchesInOrder (react-dom.js:616)
    executeDispatchesAndRelease (react-dom.js:719)
    forEach
    forEachAccumulated (react-dom.js:699)
    runEventsInBatch (react-dom.js:744)
    runExtractedPluginEventsInBatch (react-dom.js:875)
    handleTopLevel (react-dom.js:6026)
    dispatchEventForPluginEventSystem (react-dom.js:6121)
    dispatchEvent (react-dom.js:6150)
    dispatchEvent
    unstable_runWithPriority (react.js:2820)
    discreteUpdates$1 (react-dom.js:21810)
    discreteUpdates (react-dom.js:2357)
    dispatchDiscreteEvent (react-dom.js:6104)
    dispatchDiscreteEvent
0
philolegein 3 months 0 Answers 15 views 0

Leave an answer