# Converting Buffer object to CSV or String

**URL:** <https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363>\
**Category:** General\
**Created:** [23 April 2020 13:26 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363 "2020-04-23T13:26:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 13:26 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/1 "2020-04-23T13:26:00Z")

</div>

How do I convert the buffer object in this screenshot:

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

back to a CSV file:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35c6a3ab3098dbc1d55e254acfcdbe2afd03a8e3.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:** [23 April 2020 13:50 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/2 "2020-04-23T13:50:45Z")

</div>

where is the buffer coming from? What node is generating that data?

PS, you could use

```auto
msg.payload = msg.payload.toString()
return msg

```

but it may not be necessary (need to see your flow and what generates the data)

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 14:40 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/3 "2020-04-23T14:40:44Z")

</div>

> [@Steve-Mcl](#):
>
> msg.payload = msg.payload.toString()

This error shows when i put msg.payload.toString() -  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e479dfeb537868b75144f7c8b693ed0b86bee04.png)  
Here is my flow. The buffer comes from firebase storage. When I upload my file as a single buffer object or a utf string the output from the read node stays the same  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9de6b26b4b7c1c0aa9bf187b163fba4766063a62.png)  
The change node just takes the payload from an array to payload. Next to the buffer in the debug window i can change it from row to string which shows the correct values but when i write this buffer object to a file it does not write it correctly. so i need to change  
the buffer to a string...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f10ee99492143a2f3f204af147deb3993c48880.png)

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 15:08 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/4 "2020-04-23T15:08:07Z")

</div>

If you take a file node and the output will be a single buffer object it gives the exact same msg in the debug as from that storage read node. so how can i convert that buffer to a string ?

---

<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:** [23 April 2020 15:12 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/5 "2020-04-23T15:12:49Z")

</div>

attach a debug node to the output of "storage-read" - its no good showing anyone that is BAD after processing.

In your error "cannot read property `toString` it could be because msg.payload is empty (hence needing you to put a debug after "storage-read")

PS what node is that "storage-read"? please show me in flow library

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 15:22 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/6 "2020-04-23T15:22:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 15:23 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/7 "2020-04-23T15:23:59Z")

</div>

> **[node-red-contrib-firebase-admin](https://flows.nodered.org/node/node-red-contrib-firebase-admin)**
>
> A node-red module that wraps the server-side admin SDK of firebase, firestore, et.c.

When i try to use the node-red-contrib-firebase-storage my nodes didnt want to connect to firebase and no one could help me. So i tried this one. This one works but after every third deploy of flows the nodes stop working. something to do with credentials. then i need to restart again.

---

<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:** [23 April 2020 15:27 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/8 "2020-04-23T15:27:54Z")

</div>

so your data is a buffer (before you break it in the change node).

So get rid of the change node and put a function node in its place & put the code I said in it.

> [@Steve-Mcl](#):
>
> msg.payload = msg.payload.toString()

Heres the proof...

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

`firebase node >> function node >> debug node`

function node code...

```auto
msg.payload = msg.payload.toString()
return msg;

```

---

<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:** [23 April 2020 15:34 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/9 "2020-04-23T15:34:18Z")

</div>

PS, i dont use that node - does it not have an option to return something other than a buffer?

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 15:43 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/10 "2020-04-23T15:43:13Z")

</div>

> [@Steve-Mcl](#):
>
> msg.payload = msg.payload.toString()

When i deploy that function node the error still appears but when i restart the IOT2000 with the function node connected it works. Mindblown.... SO now everytime i need to restart the thing which takes ages..  
When im finished with this project hopefully it works continuously after the first startup

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 15:52 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/11 "2020-04-23T15:52:23Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/1921a8869526729773b713117a6dc3bad842b6ac.png)

One more issue please. Under every array there is a "name" which i want to retrieve. I know how to retrieve a name from a specific array but how can i retrieve all of them

---

<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:** [23 April 2020 15:56 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/12 "2020-04-23T15:56:41Z")

</div>

So payload is an object with a files property which is an array. You could do loop through `msg.payload.files` and push `msg.payload.files[x].name` to another array (where x is an integer incrementing from 0 to `msg.payload.files.length`)

Ps, there will also be a few other ways including JSONata and a change node - but I'm no up to speed on that.

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 18:07 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/13 "2020-04-23T18:07:30Z")

</div>

> [@Steve-Mcl](#):
>
> msg.payload.files.name

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/44fbd878ff7424bcacd527b65a9e5121df30d717.png)  
Am i doing something wrong?

---

<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:** [23 April 2020 19:05 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/14 "2020-04-23T19:05:28Z")

</div>

You're close but yes you are doing something wrong - Inside the loop, you are overwriting `msg.payload` on the first iteration with the `name` from element 0. (I.e. you're killing the array)

Try creating a temporary var array above the loop to hold the names.

```auto
var names = []

```

Then inside the loop....

```auto
names.push(msg.payload.files[i].name);

```

And lastly return the array

```auto
msg.payload = names 
return msg

```

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [23 April 2020 19:29 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/15 "2020-04-23T19:29:53Z")

</div>

> [@Steve-Mcl](#):
>
> msg.payload = names

Awesome. Thank you so much 😀

---

<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:** [7 May 2020 19:29 UTC](https://discourse.nodered.org/t/converting-buffer-object-to-csv-or-string/25363/16 "2020-05-07T19:29:54Z")

</div>

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