# Why this code inserts two items?

**URL:** <https://forum.wixstudio.com/t/why-this-code-inserts-two-items/52042>\
**Category:** Ask the community\
**Tags:** technical-talk, question, code\
**Created:** [August 30, 2023, 8:03pm UTC](https://forum.wixstudio.com/t/why-this-code-inserts-two-items/52042 "2023-08-30T20:03:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShowMeTheMoney](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/showmethemoney/32/82079_2.png) [@ShowMeTheMoney](https://forum.wixstudio.com/u/ShowMeTheMoney)\
**Post date:** [August 30, 2023, 8:03pm UTC](https://forum.wixstudio.com/t/why-this-code-inserts-two-items/52042/1 "2023-08-30T20:03:28Z")

</div>

I added a new empty page and wrote this simple code below, to insert a log into a collection, here is the result:

- only one ‘$w.onReady called’ printed
- only one ‘dump\_log called’ printed
- TWO ITEMS with same value except \_id inserted into the collection

I tried different collection, I deleted async/await, I moved the code to another page wether it’s new or old, all the same result!!!  
Really can’t figure why this happens 😌, could anyone help me?

```javascript
import wixData from 'wix-data';

async function dump_log(msg) {
    console.log('dump_log called');
    let mmm = {
        title: 'test-title',
        args: 'no args',
        logType: 'log',
        logMessage: 'why???'
    };
    await wixData.insert('backendlog', mmm)
        .then((result) => {})
        .catch((error) => {
            console.error(error.message);
        });
}

$w.onReady(async function () {
    console.log('$w.onReady called');
    await dump_log('dump msg');
});

```

---

<div class="post-metadata">

**Author:** ![loeiks](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/loeiks/32/109570_2.png) [@loeiks](https://forum.wixstudio.com/u/loeiks)\
**Post date:** [August 30, 2023, 8:43pm UTC](https://forum.wixstudio.com/t/why-this-code-inserts-two-items/52042/2 "2023-08-30T20:43:26Z")

</div>

In fact it’s very easy to understand why it’s also very normal that why you didn’t understand.

**Here is the reason why it’s creating two same data:**  
When you load a page in Wix websites page renders twice. First render happens on backend (server side) and the next one happens on client side (your browser) and your code executes two times because of this.

→ If you want to learn more about this you can read docs [here](https://www.wix.com/velo/reference/$w/onready).

If you want to run your code once you can use [rendering APIs](https://www.wix.com/velo/reference/wix-window-frontend/rendering) to block code running twice when you are loading the page. You can also use this as an advantage to create SSR.

[Here is a blog post](https://shoonia.site/ssr-and-warmup-data/) about how you can use this with **SSR**. by @alexanderz61239

* * *

> If this helps you can mark my answer as [solution](https://prnt.sc/yaV9vh5nAc2k).
