# Code only works in preview mode(updating database)

**URL:** <https://forum.wixstudio.com/t/code-only-works-in-preview-mode-updating-database/5874>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [February 4, 2023, 5:23am UTC](https://forum.wixstudio.com/t/code-only-works-in-preview-mode-updating-database/5874 "2023-02-04T05:23:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![qsqcqs](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/qsqcqs/32/36422_2.png) [@qsqcqs](https://forum.wixstudio.com/u/qsqcqs)\
**Post date:** [February 4, 2023, 5:23am UTC](https://forum.wixstudio.com/t/code-only-works-in-preview-mode-updating-database/5874/1 "2023-02-04T05:23:12Z")

</div>

This code was put into masterPage.js but only works when run in preview mode. The ip is obtained when run normally but the database is not updating.

```javascript
import { 

    fetch 
} 
**from**'wix-fetch' ; 
import wixData **from**'wix-data' ; 

$w . onReady ( **function** () { 
    
    fetch (<ip getting website>, { 

            method : 'get' 

        }) 

        . then (( httpResponse ) => { 

            **if** ( httpResponse . ok ) { 

                **return** httpResponse . json (); 

            } 

        }) 

        . then (( json ) => { 
            
            **const** ipaddress = json . query ; 
            **let** city = json . city ; 
            **if** ( city !== **null** && city !== '' ) 
            { 
                //$w("#text6").text = json.query; //set city name for text27 in frontend. 
            **let** toInsert = { 
  
  
  "ip" : json . query 
  
}; 
                //$w("#text6").text = json.query; //set city name is blank or null then it will show blank. 
wixData . insert ( "ips" , toInsert ). then (( item ) => { 
console . log ( item ); //see item below 
    }) 
  . **catch** (( err ) => { 
    console . log ( err ); 
  }); 
            } 
            **else** 
            { 

                //return '';\ 
                **let** toInsert = { 
  
  
  "ip" : json . query 
  
}; 
                //$w("#text6").text = json.query; //set city name is blank or null then it will show blank. 
wixData . insert ( "ips" , toInsert ). then (( item ) => { 
console . log ( item ); //see item below 
    }) 
  . **catch** (( err ) => { 
    console . log ( err ); 
  }); 
            } 
            // return ipaddress; 
        }); 

}); 

```
