pedro davim
pedro davim

Reputation: 27

node.js + wordpress return post data

i'm developing a node.js server to get external api data, and then use my client side to fetch the data from my node server, instead of making the call to the api sites.

i'm using express, axios, https

i ve created the endpoints for my node.js

one of the working example: const URL3 = https://strainapi.evanbusse.com/${STAIN_API}/strains/search/all;


var myDta = https.get(URL3, (resp) => {
    let i = 1;
    let data = '';

    resp.on('data', (chunk) => {
        data += chunk;
    });
    resp.on('end', () => {

        strains = JSON.parse(data)
        return strains
    });
}).on("error", (err) => {
    console.log("Error: " + err.message);
});

var strains = myDta


app.get("/orders", (req, res, next) => {
    res.json(myDta);
})

and the endpoint works fine in node.js.

Now, for getting the wordpress post i m doing:

const URL1='https://example.com/wp-json/wp/v2/posts'

var postData = axios.get(URL1)
    .then(response => response.data)
    .then((data) => {
        pushPost = [];
        k = data[0]
        pushPost.push(k)
        console.log('my data ', pushPost) //This gives me the post data
        return pushPost
    })

var postDataRes = postData

console.log('this pushpost ', postDataRes) 
//this gives me an empty array

app.get("/posts", (req, res, next) => {
    res.json(postDataRes)
})

but i'm getting and empty object, but in the console i can get the post i want, using :

console.log('my data ', pushPost)

can't understand why i cant push the post data to the endpoint in my node.js server.

Upvotes: 0

Views: 952

Answers (2)

leah
leah

Reputation: 1

I do something opposite I want to receive data from node.js in WordPress The problem is that I get this error WP_Error Object ( [errors] => Array ( [http_request_failed] => Array ( [0] => cURL error 7: Failed to connect to localhost port 3040: Connection refused )

     )

 [error_data] => Array
     (
     )

 [additional_data:protected] => Array
     (
     )

) Although when I make a reading in the browser I get a correct answer Only from the code in WP I get the error again This is the code in WordPress: function get_data_from_node() {

$protocol = is_SSL() ? 'https://' : 'http://';
$url =  "{$protocol}localhost:3040/data";
$response = wp_remote_get($url);
print_r($response);

echo "fvfvfvfvfv"; if (is_wp_error($response)) { return new WP_Error('node_request_failed', 'Error fetching data from Node.js server.', array('status' => 500)); }

Upvotes: 0

jameslol
jameslol

Reputation: 1935

You'll need to read up on and practice javascript promises/asynchronous execution. The functions you're defining and providing to .then() will execute after console.log('this pushpost ', postDataRes) Any data you get from a promise can only be accessed inside its .then() function.

Also, you'll probably want to do the data fetching inside your route, or it will only happen once, when the expressjs server is loaded.

const URL1='https://example.com/wp-json/wp/v2/posts'

app.get("/posts", (req, res, next) => {
    axios.get(URL1)
        .then(response => response.data)
        .then((data) => {
            const pushPost = [];
            k = data[0]
            pushPost.push(k)
            console.log('my data ', pushPost) //This gives me the post data

            res.json(pushPost)
        })

})

Upvotes: 1

Related Questions