0

is there a way i can capture image ID and use that id as a parameter to another function

function uploadImage(filePath) {

    var config = {
        method: 'post',
        url: mediaUrl,
        headers: {
            'Authorization': auth,
            'Content-Type': 'application/json',
            "Content-Disposition": 'form-data; filename="image.jpeg"',
            "Content-Type": "image/jpeg",
        },
        data: fs.readFileSync(filePath)
    };

    axios(config)
        .then(function(response) {
            const imagePostId = JSON.stringify(response.data.id);
            console.log("Image ID " + imagePostId)
            return imagePostId
        })
        .catch(function(error) {
            console.log(error);
        });
}

I want to get an id thats created when i upload an image to Wordpress and use that id in another function.

function setImageForPost(imagePostId, targetPostID) {
    const data = {
        "acf": {
            "profile_pic_url": imagePostId
        }
    }

    var config = {
        method: 'post',
        // url: mediaUrl,
        url: `http://URL/${targetPostID}`,
        headers: {
            'Authorization': auth,
            'Content-Type': 'application/json',
        },
        data: data
    };
    axios(config)
        .then(function(response) {
            console.log(JSON.stringify(response.data));
        })
        .catch(function(error) {
            console.log(error);
        });
}

there is also another function thats supposed to return postID. the end goal is to use the image id and post id to upload images to the right post. i keep returning "undefined" for imagePostId and targetPostID.

Major
  • 41
  • 2
  • Why do you have multiple content-type headers? – Phil Aug 25 '22 at 01:31
  • Simply add a `return` before `axios` then the `uploadImage()` function will resolve with a promise that returns the `imagePostId` – Phil Aug 25 '22 at 01:33

0 Answers0