# Format function for database - deviceNames

**URL:** <https://community.particle.io/t/format-function-for-database-devicenames/44972>\
**Category:** Troubleshooting\
**Created:** [November 3, 2018, 9:02pm UTC](https://community.particle.io/t/format-function-for-database-devicenames/44972 "2018-11-03T21:02:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmitchell](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gmitchell](https://community.particle.io/u/gmitchell)\
**Post date:** [November 3, 2018, 9:02pm UTC](https://community.particle.io/t/format-function-for-database-devicenames/44972/1 "2018-11-03T21:02:24Z")

</div>

Rick from tech support gave me these instructions for automatically inserting the device name into a text message. This allowed me to run the same program on multiple devices, but when one publishes and event I can recieve a message that notes which device it is coming from.

> **[rickkas7/rules-engine-examples](https://github.com/rickkas7/rules-engine-examples/tree/master/device-name-example)**
>
> Example code for the Particle Rules Engine (node red) - rickkas7/rules-engine-examples

I wanted to modify to do the same for a firebase database. I set this up to recieve all publications from all devices. The previous instructions worked for this, but the data came in as all on string…“this event happened at this time from this device”

I need it to come through as different data sets so I tried to modify the function similar to this tutorial:  
[https://docs.particle.io/tutorials/iot-rules-engine/real-time-alerting/](https://docs.particle.io/tutorials/iot-rules-engine/real-time-alerting/)

Here’s my code. and a screen shot with the error I get.

Can anyone help me format this, so that it works

var deviceNames = flow.get(‘deviceNames’, deviceNames);

msg.payload = {‘Event’: (msg.event),  
‘Payload’: (msg.payload),  
‘Time’: msg.published\_at,  
‘Device’: deviceNames[msg.device]};

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/6/6633e86f261a444ece104f1a90e794b5ee7895d9.png)

here’s a picture of the database with previous entries coming through the way I want, then the last entries are with my new flow (all one strng)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/f/3/f35c58b8e25e1adbcd0b2fe3e775b02a21ec658a.png)

---

<div class="post-metadata">

**Author:** ![gmitchell](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gmitchell](https://community.particle.io/u/gmitchell)\
**Post date:** [November 3, 2018, 10:24pm UTC](https://community.particle.io/t/format-function-for-database-devicenames/44972/2 "2018-11-03T22:24:44Z")

</div>

OK, It's working now. I had stolen parts from my test unit to send out the 7 production units, so I was trying to simulate events with CLI. When the message went out as a string originally, it just ignored the device name code and I got "Event Power on Payload Test Power on Time 2018-..." in firebase, so I figured it would do same for what I am trying to do now. I just set up a button on the device and did it the correct way and this flow works.

thanks for your help

I wanted to try to set up Mongol and try that too, but I am having a hard time. Here's the link to the discussion I started on Forum:

> [@Hostname of your mongodb server](https://community.particle.io/t/hostname-of-your-mongodb-server/44971/5):
>
> Here’s the tutorial I’m trying to follow [https://docs.particle.io/tutorials/iot-rules-engine/data-management/](https://docs.particle.io/tutorials/iot-rules-engine/data-management/) . It doesn’t give much help in setting up mongol. I signed up for a trial account and created a cluster and a database and a collection. I’ve tried copying the connection string here and other stuff

If you have time and have any idea what I am doing wrong, I'd appreciate your help

Thanks again.
