WP-API / node-wpapi

An isomorphic JavaScript client for the WordPress REST API
http://wp-api.org/node-wpapi/
MIT License
1.68k stars 191 forks source link

WP.[CPT]().id() is not a function ... Cannot update CPT #497

Closed ahmedmusawir closed 2 years ago

ahmedmusawir commented 2 years ago

I've been following the documentation to the dot ... here is my code:

 const wp = new WPAPI({
    endpoint: 'http://localhost:10004/wp-json',
    username: 'cgteam',
    password: '************************',
  });
  // SETTING CPT ROUTE
  wp.flag = wp.registerRoute('wp/v2', '/flag');

  useEffect(() => {
    const fetchPosts = async () => {
      try {
        setIsPending(true);
        // UPDATE CUSTOM POST TYPE: FLAG w/ ACF
        const fetchedPosts = await wp.flag().id(1211);
        console.log(fetchedPosts);
        setPosts(fetchedPosts);
        setIsPending(false);
      } catch (e) {
        console.log(e);
        return [];
      }
    };
    fetchPosts();
  }, []);

But this is the error message I'm getting:

TypeError: wp.flag(...).id is not a function
    at _callee$ (script.min.js?ver=1640242704:567)
    at tryCatch (script.min.js?ver=1640242704:90432)
    at Generator.invoke [as _invoke] (script.min.js?ver=1640242704:90662)
    at Generator.next (script.min.js?ver=1640242704:90487)
    at asyncGeneratorStep (script.min.js?ver=1640242704:2185)
    at _next (script.min.js?ver=1640242704:2207)
    at script.min.js?ver=1640242704:2214
    at new Promise (<anonymous>)
    at script.min.js?ver=1640242704:2203
    at fetchPosts (script.min.js?ver=1640242704:592)

Creating a new custom post (flag) worked fine. But every time I try to update which requires the .id(122) function, I get it's not a function... can you plz tell me what am I doing wrong ...

image

ahmedmusawir commented 2 years ago

Fixed the mistake ... sorry!