wp.data.select('meta')属性未使用WordPress的Gutenberg定义

wp.data.select('meta')属性未使用WordPress的Gutenberg定义,wordpress,wordpress-gutenberg,Wordpress,Wordpress Gutenberg,我在WordPress中使用Gutenberg,我想在用户发布帖子之前检查一些字段 我想检查元框中的特征图像、标题和简单文本字段是否为空 如果某个字段为空,则会显示通知,并且我锁定了“发布”按钮 目前,所有作品的特色形象和标题都很好。但当我试图检查元框中的文本字段是否为空时,我发现了错误: Uncaught TypeError: Cannot read property '_myprefix_text_metafield' of undefined 我用如下文本字段创建了我的元框: impor

我在WordPress中使用Gutenberg,我想在用户发布帖子之前检查一些字段

我想检查元框中的特征图像、标题和简单文本字段是否为空

如果某个字段为空,则会显示通知,并且我锁定了“发布”按钮

目前,所有作品的特色形象和标题都很好。但当我试图检查元框中的文本字段是否为空时,我发现了错误:

Uncaught TypeError: Cannot read property '_myprefix_text_metafield' of undefined
我用如下文本字段创建了我的元框:

import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { TextControl } from '@wordpress/components';

const TextController = (props) => {
    const meta = useSelect(
        (select) =>
        select('core/editor').getEditedPostAttribute('meta')['_myprefix_text_metafield']
    );
    const { editPost } = useDispatch('core/editor');
    return (
        <TextControl
        label={__("Text Meta", "textdomain")}
        value={meta}
        onChange={(value) => editPost({ meta: { _myprefix_text_metafield: value } })}
        />
    );
};

const PluginDocumentSettingPanelDemo = () => (
    <PluginDocumentSettingPanel name="text-presentation" title="Texte de présentation" className="custom-panel custom-panel-presentation" icon=" ">
        <TextController />
    </PluginDocumentSettingPanel>
);

registerPlugin('plugin-document-setting-panel-demo', {
    render: PluginDocumentSettingPanelDemo
});
当我在控制台中编写此命令时:

wp.data.select'core/editor'。getEditedPostAttribute'meta'[''我的前缀\文本\ metafield']


该值很好地返回了我的文本字段的值。

当从RESTAPI获取post数据的XHR/AJAX请求尚未完全解析时,可能会发生这种情况,因此您不能像这样简单地访问元数据。您需要确保getEditedPostAttribute'meta'实际返回一个对象,然后才访问_myprefix_text_metafield属性

因此,请尝试以下方法:

const textPresentation=wp.data.select'core/editor'//为简洁起见,已包装 .getEditedPostAttribute“元”?。\u myprefix\u text\u元字段; //如果演示文稿为空,请锁定帖子。 锁 未定义!==文本演示&!文本演示, “演示锁”, '请添加演示文稿文本' ;
非常感谢你的帮助。那很好。如果textPresentation未定义,在beggining测试的锁定功能中,如果textPresentation不为空,页面何时完全加载?这是什么意思?在wp.data.select'core/editor.getEditedPostAttribute'meta'?.myprefix\u text\u MetaField中表示它被调用。不使用该属性的等效代码是1 const meta=wp.data.select'core/editor'。getEditedPostAttribute'meta'|{};和2个未定义!==元。_myprefix_text_元字段&&!元。_myprefix_text_元字段
const locks = [];

function lock( lockIt, handle, message ) {
    if ( lockIt ) {
        if ( ! locks[ handle ] ) {
            locks[ handle ] = true;
            wp.data.dispatch( 'core/editor' ).lockPostSaving( handle );
            wp.data.dispatch( 'core/notices' ).createNotice(
                'error',
                message,
                { id: handle, isDismissible: false }
            );
        }
    } else if ( locks[ handle ] ) {
        locks[ handle ] = false;
        wp.data.dispatch( 'core/editor' ).unlockPostSaving( handle );
        wp.data.dispatch( 'core/notices' ).removeNotice( handle );
    }
}

wp.data.subscribe( () => {
    // get presentation
    const textPresentation = wp.data.select( 'core/editor' ).getEditedPostAttribute('meta')['_myprefix_text_metafield'];

    // Lock the post if the presentation is empty.
    lock(
        ! textPresentation,
        'presentation-lock',
        'Please add a presentation text',
    );

    // get the current title
    const postTitle = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'title' );

    // Lock the post if the title is empty.
    lock(
        ! postTitle,
        'title-lock',
        'Please add a title',
    );


    // get the Featured Image
    const featuredImage = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'featured_media' );

    // Lock post if there is no Featured Image selected
    lock(
        featuredImage === 0,
        'featured-image-lock',
        'Please add a featured image',
    );

});