Gutenberg: Why are selectors missing from Wordpress core data store

Created on 6 Jul 2019  路  7Comments  路  Source: WordPress/gutenberg

When selecting the "core" store of Wordpress JavaScript API via wp.data.select("core") It returns an incomplete list of selectors.

The selectors returned are the following:

canUser: 茠 ()
getAuthors: 茠 ()
getEmbedPreview: 茠 ()
getEntitiesByKind: 茠 ()
getEntity: 茠 ()
getEntityRecord: 茠 ()
getEntityRecords: 茠 ()
getMedia: 茠 ()
getMediaItems: 茠 ()
getPostType: 茠 ()
getPostTypes: 茠 ()
getTaxonomies: 茠 ()
getTaxonomy: 茠 ()
getThemeSupports: 茠 ()
getUserQueryResults: 茠 ()
hasUploadPermissions: 茠 ()
isPreviewEmbedFallback: 茠 ()
isRequestingEmbedPreview: 茠 ()

Instead of the full lists shown in https://developer.wordpress.org/block-editor/data/data-core/

For example, wp.data.select("core").getCurrentUser() returns undefined when called from the edit function of a block declared inside a regiterBlockType function in a JS script registered in the init hook.

[Package] Core data [Type] Help Request

Most helpful comment

I can see all selectors in my console:
core-data

For resolving the undefined problem, please see https://github.com/WordPress/gutenberg/issues/14623#issuecomment-499445665

All 7 comments

I can see all selectors in my console:
core-data

For resolving the undefined problem, please see https://github.com/WordPress/gutenberg/issues/14623#issuecomment-499445665

I'm trying to show some fields in a block based on user capabilities (only visible for admins). From what I understood from the post, I should be using withSelect HOC to inject, in this case, what would be the user object returnet by wp.data.select('core').getCurrentUser() as a prop for my component. Doing this still returns undefined, what makes me think i'm missing out something here. Still, when evaluating wp.data.select('core') directly in the console i get this list :
image
intead of the list shown by @Soean, but it's the same list that @Pretzel63 got in #14623 what makes me think we both were doing something wrong.
This is what I'm trying to do:

`const { withSelect } = wp.data;

function MyComponent ({currentUser}) {
// use current user
// return corresponding fields for either admins or normal users
}

export default withSelect( ( select ) => ( {
currentUser: select( 'core' ).getCurrentUser(),
} ) )( MyComponent );
`

Can I get some insight of what I could be doing wrong?

I know this is closed, but @TKY2048 I had the same issue as you and after digging around for a while I found out it's because it's not in the core build of WP yet. Hopefully this helps.

I found a workaround, got the expected behaviour with:

export default withSelect( select => ( { userIsAdmin: select( 'core' ).canUser( 'create', 'users' ), } ) )( MyComponent );

@stuartshields there seems still to be a discrepancy regarding available methods when doing wp.data.select("core") in the browser console versus what is documented on https://developer.wordpress.org/block-editor/data/data-core/.

E.g. the method getPostTypes is not documented. You wrote, it's not part of the core build, do you know, in which repo it is located then?

getPostTypes and the like are derived from the getEntity selectors I think. They are not documented individually due to repetition

ah ok.. funny enough, I also don't really find any usages in this repo when I search for the method..just two within tests..

Was this page helpful?
0 / 5 - 0 ratings