Trying to do an api fetch using id from earlier api fetch










0















So I've tried to find information regarding this and I'm feeling quite stuck.
I'm running headless wordpress using react and I have a page who fetch my single post and return it.



Every post (custom post type called Tjanster) has an acf field with a relation to another custom post type which is the Person associated with the post.



Right now everything works with getting the post content as well as the person object with ID and slug etc. But I need the ACF content from that "person post type.



My idea right now is that I acquire the ID from the person object in the post and then do another api-fetch for that id directly towards the person endpoint, is that correct thinking?



This is my post json that I get with the acf.person[0].id from (http://localhost:8080/wp-json/wp/v2/tjanster)



enter image description here



Here I get the ID of the person which is 68 and then I can do a request to http://localhost:8080/wp-json/wp/v2/person/68 which returns the object with the correct acf info:



enter image description here



And this is how my tjanster.js (single-post-page) looks right now:



import Layout from "../components/Layout.js";
import React, Component from "react";
import fetch from "isomorphic-unfetch";
import Error from "next/error";
import PageWrapper from "../components/PageWrapper.js";
import Menu from "../components/Menu.js";
import Config from "../config.js";

class Tjanster extends Component
static async getInitialProps(context)
const slug, apiRoute = context.query;

const res = await fetch(
`$Config.apiUrl/wp-json/postlight/v1/$apiRoute?slug=$slug`
);
const tjanster = await res.json();

return tjanster ;



render()
if (!this.props.tjanster.title) return <Error statusCode=404 />;

let personId = this.props.tjanster.acf.person[0].ID;
console.log(personId);

return (
<Layout>

this.props.tjanster.title.rendered

</Layout>
);



export default PageWrapper(Tjanster);


As you can see I have the id of the person object saved under render as personId but I'm not entirely sure what to do next? I figured I could do another fetch at the top using $Config.apiUrl/wp-json/wp/v2/person/$personId but I won't get that to work since I can't access the ID yet.



Any ideas would be greatly appreciated!










share|improve this question


























    0















    So I've tried to find information regarding this and I'm feeling quite stuck.
    I'm running headless wordpress using react and I have a page who fetch my single post and return it.



    Every post (custom post type called Tjanster) has an acf field with a relation to another custom post type which is the Person associated with the post.



    Right now everything works with getting the post content as well as the person object with ID and slug etc. But I need the ACF content from that "person post type.



    My idea right now is that I acquire the ID from the person object in the post and then do another api-fetch for that id directly towards the person endpoint, is that correct thinking?



    This is my post json that I get with the acf.person[0].id from (http://localhost:8080/wp-json/wp/v2/tjanster)



    enter image description here



    Here I get the ID of the person which is 68 and then I can do a request to http://localhost:8080/wp-json/wp/v2/person/68 which returns the object with the correct acf info:



    enter image description here



    And this is how my tjanster.js (single-post-page) looks right now:



    import Layout from "../components/Layout.js";
    import React, Component from "react";
    import fetch from "isomorphic-unfetch";
    import Error from "next/error";
    import PageWrapper from "../components/PageWrapper.js";
    import Menu from "../components/Menu.js";
    import Config from "../config.js";

    class Tjanster extends Component
    static async getInitialProps(context)
    const slug, apiRoute = context.query;

    const res = await fetch(
    `$Config.apiUrl/wp-json/postlight/v1/$apiRoute?slug=$slug`
    );
    const tjanster = await res.json();

    return tjanster ;



    render()
    if (!this.props.tjanster.title) return <Error statusCode=404 />;

    let personId = this.props.tjanster.acf.person[0].ID;
    console.log(personId);

    return (
    <Layout>

    this.props.tjanster.title.rendered

    </Layout>
    );



    export default PageWrapper(Tjanster);


    As you can see I have the id of the person object saved under render as personId but I'm not entirely sure what to do next? I figured I could do another fetch at the top using $Config.apiUrl/wp-json/wp/v2/person/$personId but I won't get that to work since I can't access the ID yet.



    Any ideas would be greatly appreciated!










    share|improve this question
























      0












      0








      0








      So I've tried to find information regarding this and I'm feeling quite stuck.
      I'm running headless wordpress using react and I have a page who fetch my single post and return it.



      Every post (custom post type called Tjanster) has an acf field with a relation to another custom post type which is the Person associated with the post.



      Right now everything works with getting the post content as well as the person object with ID and slug etc. But I need the ACF content from that "person post type.



      My idea right now is that I acquire the ID from the person object in the post and then do another api-fetch for that id directly towards the person endpoint, is that correct thinking?



      This is my post json that I get with the acf.person[0].id from (http://localhost:8080/wp-json/wp/v2/tjanster)



      enter image description here



      Here I get the ID of the person which is 68 and then I can do a request to http://localhost:8080/wp-json/wp/v2/person/68 which returns the object with the correct acf info:



      enter image description here



      And this is how my tjanster.js (single-post-page) looks right now:



      import Layout from "../components/Layout.js";
      import React, Component from "react";
      import fetch from "isomorphic-unfetch";
      import Error from "next/error";
      import PageWrapper from "../components/PageWrapper.js";
      import Menu from "../components/Menu.js";
      import Config from "../config.js";

      class Tjanster extends Component
      static async getInitialProps(context)
      const slug, apiRoute = context.query;

      const res = await fetch(
      `$Config.apiUrl/wp-json/postlight/v1/$apiRoute?slug=$slug`
      );
      const tjanster = await res.json();

      return tjanster ;



      render()
      if (!this.props.tjanster.title) return <Error statusCode=404 />;

      let personId = this.props.tjanster.acf.person[0].ID;
      console.log(personId);

      return (
      <Layout>

      this.props.tjanster.title.rendered

      </Layout>
      );



      export default PageWrapper(Tjanster);


      As you can see I have the id of the person object saved under render as personId but I'm not entirely sure what to do next? I figured I could do another fetch at the top using $Config.apiUrl/wp-json/wp/v2/person/$personId but I won't get that to work since I can't access the ID yet.



      Any ideas would be greatly appreciated!










      share|improve this question














      So I've tried to find information regarding this and I'm feeling quite stuck.
      I'm running headless wordpress using react and I have a page who fetch my single post and return it.



      Every post (custom post type called Tjanster) has an acf field with a relation to another custom post type which is the Person associated with the post.



      Right now everything works with getting the post content as well as the person object with ID and slug etc. But I need the ACF content from that "person post type.



      My idea right now is that I acquire the ID from the person object in the post and then do another api-fetch for that id directly towards the person endpoint, is that correct thinking?



      This is my post json that I get with the acf.person[0].id from (http://localhost:8080/wp-json/wp/v2/tjanster)



      enter image description here



      Here I get the ID of the person which is 68 and then I can do a request to http://localhost:8080/wp-json/wp/v2/person/68 which returns the object with the correct acf info:



      enter image description here



      And this is how my tjanster.js (single-post-page) looks right now:



      import Layout from "../components/Layout.js";
      import React, Component from "react";
      import fetch from "isomorphic-unfetch";
      import Error from "next/error";
      import PageWrapper from "../components/PageWrapper.js";
      import Menu from "../components/Menu.js";
      import Config from "../config.js";

      class Tjanster extends Component
      static async getInitialProps(context)
      const slug, apiRoute = context.query;

      const res = await fetch(
      `$Config.apiUrl/wp-json/postlight/v1/$apiRoute?slug=$slug`
      );
      const tjanster = await res.json();

      return tjanster ;



      render()
      if (!this.props.tjanster.title) return <Error statusCode=404 />;

      let personId = this.props.tjanster.acf.person[0].ID;
      console.log(personId);

      return (
      <Layout>

      this.props.tjanster.title.rendered

      </Layout>
      );



      export default PageWrapper(Tjanster);


      As you can see I have the id of the person object saved under render as personId but I'm not entirely sure what to do next? I figured I could do another fetch at the top using $Config.apiUrl/wp-json/wp/v2/person/$personId but I won't get that to work since I can't access the ID yet.



      Any ideas would be greatly appreciated!







      wordpress reactjs rest api headless






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 15:16









      TriphysTriphys

      285




      285






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to use javascript Promise to handle multiple asynchronous calls. Just chain a then after your first fetch, you will be able to access to the Id when it will be available.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303400%2ftrying-to-do-an-api-fetch-using-id-from-earlier-api-fetch%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You need to use javascript Promise to handle multiple asynchronous calls. Just chain a then after your first fetch, you will be able to access to the Id when it will be available.






            share|improve this answer





























              1














              You need to use javascript Promise to handle multiple asynchronous calls. Just chain a then after your first fetch, you will be able to access to the Id when it will be available.






              share|improve this answer



























                1












                1








                1







                You need to use javascript Promise to handle multiple asynchronous calls. Just chain a then after your first fetch, you will be able to access to the Id when it will be available.






                share|improve this answer















                You need to use javascript Promise to handle multiple asynchronous calls. Just chain a then after your first fetch, you will be able to access to the Id when it will be available.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 11:54

























                answered Nov 14 '18 at 15:28









                Vincent BVincent B

                42137




                42137





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303400%2ftrying-to-do-an-api-fetch-using-id-from-earlier-api-fetch%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Darth Vader #20

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Ondo