# Cannot create property '\_id' on string

**URL:** <https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739>\
**Category:** General\
**Created:** [5 June 2021 13:46 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739 "2021-06-05T13:46:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [5 June 2021 13:46 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/1 "2021-06-05T13:46:22Z")

</div>

Hello,

I want to fill a database with random numbers using MongoDB. There are 3 nodes, 1 timestamp, 1 function where the random numbers are created and returned as a msg, 1 collection node of MongoDB. I have adjusted my MongoDB settings but the problem seems to exist in the function node. There is a type error "can not create property '\_id' on string" in which I am not using the id right.

-All nodes are connected with each other.

Thanks for your time, appreciate your help.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 June 2021 13:59 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/2 "2021-06-05T13:59:51Z")

</div>

msg.payload is a number, i.e a timestamp.  
It is not an object so you can not add properies to it.  
Either set it as a object i.e `msg.payload = {};` you would need to move the timestamp to another var first i.e `var hold = msg.payload;` or it will be overwritten  
Then you can do  
`msg.payload._id = 1234;`

or

you can do it in one go  
`msg.payload = {_id:1234,timestamp: msg.payload`};`  
which will make msg.payload an object with the properties \_id and timestamp.

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [5 June 2021 14:09 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/3 "2021-06-05T14:09:40Z")

</div>

Thanks for your reply, I am a beginner with Node-Red and I tried to implement your second solution but I still get this error. Can you please help me again? Thank you!

![MicrosoftTeams-image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/7715747785e8d933227f9b1af69157f4f50fe5c2.png)

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

Data generation code:  
msg.payload=(Math.round(Math.random()\*1000))/100;  
msg.payload = {​​​​​​​\_id:1234,timestamp: msg.payload}​​​​​​​;  
return [msg];

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 June 2021 14:16 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/4 "2021-06-05T14:16:59Z")

</div>

The error is from your collection node, it is probably looking for msg.\_id  
Try

```auto
​​​​​​​​​​​​​​msg.payload=(Math.round(Math.random()*1000))/100;
msg._id = msg.payload;
return msg;

```

I would think though that the input is msg.payload, what is the collection node and where is the help info/readme

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [5 June 2021 14:24 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/5 "2021-06-05T14:24:15Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/589293a939128b352ff1d8faa18fcb093fc37377.png)

I tried it but I still get the same error on MongoDB. I have attached an image of the MongoDB settings. Thanks for your help, I really appreciate it.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 June 2021 14:45 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/6 "2021-06-05T14:45:48Z")

</div>

you need to select msg.payload in the settings  
and the function should return an object. you do not need to set msg.id unless you are over writing an existing entry.  
try

```auto
​​​​​​​​​​​​​​msg.payload={payload: (Math.round(Math.random()*1000))/100};
return msg;

```

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [5 June 2021 16:19 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/7 "2021-06-05T16:19:22Z")

</div>

Thanks a lot !

---

<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:** [19 June 2021 16:19 UTC](https://discourse.nodered.org/t/cannot-create-property-id-on-string/46739/8 "2021-06-19T16:19:42Z")

</div>

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