Msg.payload always give me false?

I write following code in a "function node". I can download image file and write to file but msg.payload always is false. Why ? :roll_eyes:

const axios = global.get('axios');
const fs = global.get('fs');
var msg={"payload":false};

async function downloadImage(url, filename) {
    const response = await axios.get(url, { responseType: 'arraybuffer' });

    fs.writeFile(filename, response.data, (err) => {
        if (err) {
            
        }
        else {            
            msg.payload = true;        
        }        
    });
}

downloadImage('https://api.telegram.org/file/bot1776047125:AAGFueEEbGMejFuplD4zsUjB5Eu3BnzvMTo/photos/file_24.jpg', 'd:/temp/image.jpg');
return msg;

Hard to say but there are many things your script need to be reliable. For example, you dont check the response code. You dont do anything with the err object

However, since we are in Node-RED (low-code/no-code) I am wondering why you are using axios?

Why not simply do this:
image

And if it is because you want a means of resuing (not duplicating flows elements) then use a subflow or subroutine

2 Likes

Can you share your flow json to me ? :+1:

I never deployed it / didn't keep it.

Not difficult to create though - give it a go.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.