# Help loading Byte Array for SQL insert

**URL:** https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843
**Category:** General
**Tags:** node-red-contrib-mssql-plus
**Created:** [21 June 2024 16:31 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843 "2024-06-21T16:31:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [21 June 2024 16:31 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843/1 "2024-06-21T16:31:36Z")

</div>

Happy Friday to all.

I am using OpenAI to do some analysis on a photo taken by a RPi camera. With the help provided in [this post](https://discourse.nodered.org/t/noob-question-re-using-a-node-js-script-in-a-function-node/88535), I got that all figured out by using a function node.

Now I wish to send several pieces of data into MSSQL via a stored procedure, and one such piece is a small image. My understanding is that this image passed from Node-red to SQL should be in the form of a “byte array”. The MSSQL stored procedure specifies it as `varbinary(max)`.

Here's where I need help. As shown by these 3 debug nodes, the `msg.readingimage` is being dropped in the flow after the `function 1` node. How can I keep `msg.readingimage` intact and have it be an input into the MSSQL-Plus node? Perhaps just modifying function 1 below? Or maybe `function 3` is completely the wrong way to create a byte array?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/3305622b5ae67c8a2330063cc314f3f3320a7786.png)

function 3 (this is supposed to convert it to byte array)

```auto
msg.readingimage = [...msg.readingimage];
return msg;

```

function 1 (this sends the image & question to OpenAI):

```auto
const OAI = new openai.OpenAI({ apiKey: 'sk-myAPIkey' });

// Function to encode the image
function encodeImage(imagePath) {
    const image = fs.readFileSync(imagePath);
    const base64Image = Buffer.from(image).toString('base64');
    return `data:image/jpeg;base64,${base64Image}`;
}

// Path to the image
const imagePath = '/home/rpi0e/test1.png';

// Get the data URL
const dataUrl = encodeImage(imagePath);

async function main() {
    const response = await OAI.chat.completions.create({
        model: "gpt-4-vision-preview",
        messages: [
            {
                role: "user",
                content: [
                    { type: "text", text: "Which five digits are in this image? Return the message.content as only the five digits with no additional text."
                    },
                    {
                        type: "image_url",
                        image_url: {
                            url:dataUrl
                    },
                    },
                ],
            },
        ],
    });
    node.send({ payload: response.choices[0] })
}
main();

```

Attached is a sample image in case it helps.  
 ![test1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73983ecbccb408b0759f5cb471fde2d9c275b93a.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 June 2024 16:56 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843/2 "2024-06-21T16:56:20Z")

</div>

As far as I remember, you should simply be able to send the buffer (unmodified)

Your image does show where MSSQL-PLUS node is but i assume after `function 1`?

Since you already have (had) it in `msg.reading` simply set the parameter in the MSSQL nodes UI to use `msg.reading` as the parameter value!

---

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [21 June 2024 17:38 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843/3 "2024-06-21T17:38:38Z")

</div>

Thank you @Steve-Mcl, but passing `msg.readingimage` into the input of the stored procedure does not work.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/789fe2bc03a831032264b9d56244e9f262ed4bb1.png)

Debug with complete msg object after the MSSQL-Plus node indicates everything was fine except for the ReadingImage.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82d14f0f5b2c450f1ed7b18fae552ba09e050ca1.png)

It seems that function 1 is not allowing the byte array to be passed as `msg.readingimage`

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [21 June 2024 19:09 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843/4 "2024-06-21T19:09:36Z")

</div>

> [@grant1](#):
>
> `node.send({ payload: response.choices[0] })`

This send a new object. Instead, set msg.payload and send the original msg

---

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [21 June 2024 20:30 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843/5 "2024-06-21T20:30:41Z")

</div>

Thanks @Steve-Mcl

I ended up using a join node and combined the two payloads into an array and as you remembered, I simply sent the buffer (unmodified) into MSSQL-PLUS and it worked.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [5 July 2024 20:31 UTC](https://discourse.nodered.org/t/help-loading-byte-array-for-sql-insert/88843/6 "2024-07-05T20:31:29Z")

</div>

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