wix-data query and insert not working on the live site

In my website I collect the requirement from users using a form. This gets inserted into a database collection. This happens perfectly both in Preview and Live sites.

Once the form is submitted by the user, I use wix-data query to calculate few things based on the user input and insert this into another data collection. When the button is submitted it takes to another page where I display the order details from the second database collection.

This works perfectly in preview site, but it doesn’t work on the Live site.

The site name is - https://urcups.com.au/

Below is code snippet which I am using to populate the second database collection.
Please let me know what is going wrong.

import wixUsers from ‘wix-users’;

import wixData from ‘wix-data’;

$w.onReady(function () {

$w("#dataset1").onBeforeSave( () => { 

// Getting the current Item from the user submission
var itemObj = $w(“#dataset1”).getCurrentItem();

	console.log(itemObj._id); 

// Generating an Order number

var randomString = function (len, bits) 
{ 
bits = bits || 36; 
var outStr = "", newStr; 
while (outStr.length < len) 
	{ 
    newStr = Math.random().toString(bits).slice(2); 
    outStr += newStr.slice(0, Math.min(newStr.length, (len - outStr.length))); 
	} 
return outStr.toUpperCase(); 
}; 

var OrderNumber=randomString(6,16); 

// Retreiving the User id

var UserId = wixUsers.currentUser.id; 

//Retrieving the customer choice which happens in the previous page

		wixData.query("CustomerChoice") 
		 .eq("customerId", UserId) 
		 .or (wixData.query("CustomerChoice") 
		 	  .eq("customerId","00000000000a") //default record on Customer choice 
		 ) 
		 .ne("designUrl","OwnLogo") 
		 .descending("_updatedDate") 
		 .descending("customerId") 
		 .descending("designUrl") 
		 .limit(1) 
		 .find() 
		 .then( (results) => { 
		 	
		 let items = results.items; 
     	 var Order = items[0]; 
     	 var Design = Order.designUrl; 
   		 console.log(Design);  
 	
	// Querying the price of the cup based on the customer choice 

		 wixData.query("Cups") 
  			 .eq("title",Design) 
  			 .descending("_updatedDate") 
   			 .limit(1) 
   			 .find() 
   			 .then( (results) => { 
 		 
   			 let items2 = results.items; 
	     	 var Cup = items2[0]; 
	     	 var DesignImage = Cup.image; 
	     	 var EozSwPrice = Number(Cup.eswPrice.slice(1,3)); 
	     	 var TozSwPrice = Number(Cup.tswPrice.slice(1,3)); 
	     	 var SozSwPrice = Number(Cup.sswPrice.slice(1,3)); 
	     	 var EozDwPrice = Number(Cup.edwPrice.slice(1,3)); 
	     	 var TozDwPrice = Number(Cup.tdwPrice.slice(1,3)); 
	     	 var SozDwPrice = Number(Cup.sdwPrice.slice(1,3)); 
	   		 console.log(DesignImage);  
	   		 console.log(EozSwPrice); 
	   		 console.log(TozSwPrice); 
	   		 console.log(SozSwPrice); 
	   		 console.log(EozDwPrice); 
	   		 console.log(TozDwPrice); 
	   		 console.log(SozDwPrice); 
	   		 
	   		 console.log(itemObj); 
	   		 
	   		 if (itemObj.eozSel === true) { 
	   		 	
	   		 	if (itemObj.eozType === 'SingleWall') { 
	   		 		
	   		 		var EozAmt = Number(itemObj.eozQty)*Number(EozSwPrice)/500; 
	   		 	} 
	   		 	else if (itemObj.eozType === 'DoubleWall') { 
	   		 		 var EozAmt = Number(itemObj.eozQty)*Number(EozDwPrice)/500;	   		 		 
	   		 	} 

	   		 } 
     		 else { 
     		 	 var EozAmt = 0; 
     		 } 
     		 
	   		 if (itemObj.tozSel === true) { 
	   		 	
	   		 	if (itemObj.tozType === 'SingleWall') { 
	   		 		
	   		 		var TozAmt = Number(itemObj.tozQty)*Number(TozSwPrice)/500; 
	   		 	} 
	   		 	else if (itemObj.tozType === 'DoubleWall') { 
	   		 		 var TozAmt = Number(itemObj.tozQty)*Number(TozDwPrice)/500;	   		 		 
	   		 	} 

	   		 } 
     		 else { 
     		 	 var TozAmt = 0; 
     		 } 
	
	   		 if (itemObj.sozSel === true) { 
	   		 	
	   		 	if (itemObj.sozType === 'SingleWall') { 
	   		 		
	   		 		var SozAmt = Number(itemObj.sozQty)*Number(SozSwPrice)/500; 
	   		 	} 
	   		 	else if (itemObj.sozType === 'DoubleWall') { 
	   		 		 var SozAmt = Number(itemObj.sozQty)*Number(SozDwPrice)/500;	   		 		 
	   		 	} 

	   		 } 
     		 else { 
     		 	 var SozAmt = 0; 
     		 } 
     		 

     		 console.log (EozAmt); 
     		 console.log (TozAmt); 
     		 console.log (SozAmt); 
     		 
     		 var SubTotAmt = Number(EozAmt+TozAmt+SozAmt);	      		 	     		     		  
     		 var GSTAmt = Number(SubTotAmt/10); 
     		 var TotAmtNum = Number(SubTotAmt+GSTAmt); 
     		 
     		 console.log (SubTotAmt);					 
     		 console.log (GSTAmt); 
     		 console.log (TotAmtNum); 
     		 
     		 let CurrSymbol = 'AU$ '; 
     		 
     		 let EozAmtStr = String(EozAmt); 
     		 var dEozAmt = CurrSymbol.concat(EozAmtStr); 
     		 
     		 let TozAmtStr = String(TozAmt); 
     		 var dTozAmt = CurrSymbol.concat(TozAmtStr); 
     		 
     		 let SozAmtStr = String(SozAmt); 
     		 var dSozAmt = CurrSymbol.concat(SozAmtStr); 
     		 
     		 let SubTotAmtStr = String(SubTotAmt); 
     		 var OSubTotAmt = CurrSymbol.concat(SubTotAmtStr); 
     		 
     		 var OShippingAmt = 'AU$ 0'; 
     		 
     		 var GSTAmtStr = String(GSTAmt); 
     		 var OGSTAmt = CurrSymbol.concat(GSTAmtStr); 
     		 
     		 let TotAmtStr = String(TotAmtNum);    		  
     		 var OTotAmt = CurrSymbol.concat(TotAmtStr); 
     		 
     		 if (itemObj.eozSel === true) { 
     		 	
     		 	var OeozType = itemObj.eozType; 
     		 	var OeozQty = String(itemObj.eozQty); 
     		 	var OeozAmt = dEozAmt; 
     		 	
 		 		} 
 		 		else { 
 		 			var OeozType = ''; 
 		 			var OeozQty = ''; 
 		 			var OeozAmt = ''; 
 		 		} 
 		 		
     		 if (itemObj.tozSel === true) { 
     		 	
     		 	var OtozType = itemObj.tozType; 
     		 	var OtozQty = String(itemObj.tozQty); 
     		 	var OtozAmt = dTozAmt; 
     		 	
 		 		} 
 		 		else { 
 		 			var OtozType = ''; 
 		 			var OtozQty = ''; 
 		 			var OtozAmt = ''; 
 		 		} 

     		 if (itemObj.sozSel === true) { 
     		 	
     		 	var OsozType = itemObj.sozType; 
     		 	var OsozQty = String(itemObj.sozQty); 
     		 	var OsozAmt = dSozAmt; 
     		 	
 		 		} 
 		 		else { 
 		 			var OsozType = ''; 
 		 			var OsozQty = ''; 
 		 			var OsozAmt = ''; 
 		 		} 

   			     		 
   			 	let toInsert2 = { 
  					"title": OrderNumber 
  					,"customerId": UserId 
  					,"design":Design 
  					,"designImage":DesignImage 
  					,"customerName":itemObj.contactName 
  					,"eozType":OeozType 
  					,"eozQty":OeozQty 
  					,"eozAmt":OeozAmt 
  					,"tozType":OtozType 
  					,"tozQty":OtozQty 
  					,"tozAmt":OtozAmt 
  					,"sozType":OsozType 
  					,"sozQty":OsozQty 
  					,"sozAmt":OsozAmt 
  					,"subTotAmt":OSubTotAmt 
  					,"shippingAmt":OShippingAmt 
  					,"gstAmt":OGSTAmt 
  					,"totAmt":OTotAmt 
					}; 
				
				console.log(toInsert2); 
				
				wixData.insert("test",toInsert2);  
				
		   	} ); 
		     		 
		} ); 
			

} ); 

});

Hey Sadiq,

First of all, I’ve gotta have a cup of coffee now. :slightly_smiling_face:

I cloned your site (so I wouldn’t interfere with you) and tried it out in both Preview and Live. Everything works. Maybe I just have that magic touch?

Is there a specific scenario that doesn’t work, or is it something that always happens.

Yisrael

Thanks Yisrael. Your magic touch worked. All good now. Really appreciate your help. Cheers - Sadiq

Glad I could help Sadiq. I love it when I fix things without actually doing anything. :upside_down_face:

I’m having a problem. I have a form on my website that collects information from users, and sends them to the database. I have my code set up to check for duplicates(so people can’t create the same account twice) - now everything works great in Preview mode, i can add new users etc BUT…when I try to do it in live mode, it will not insert the information into the database. I’ve checked everything and I can’t find what the issue is. Any help would be greatly appreciated. I’m kind of at a stand still now…

import wixData from ‘wix-data’;

export function users_beforeInsert(item, context) {

//all log messages will show when we run this code. 
//they will help us debug and see all is working well. 
//they have no impact other than that and can be safely removed 
//once we see the code is working well 
console.log("in before insert hook"); 
console.log("username is " + item.username); 
console.log("email is " + item.email); 
console.log("password is " + item.password); 

//create a new promise object.  
//in the promise's body we'll look for dups. 
//if we find any, we will reject the promise and abort the insert. 
let p = new Promise(function(resolve, reject) 
{ 
	console.log("in promise body"); 
	
	//create a query to look for items with the same name as the item we're tring to insert 
	let username_q = wixData.query("users").eq("username", item.username); 
	//create a query to look for items with the same email as the item we're tring to insert 
	let email_q = wixData.query("users").eq("email", item.email); 
	//create a query to look for items with the same name OR the same number OR the same email as the item we're tring to insert 
	let big_q = username_q.or(email_q); 
	
	//run the query to count the number of items that match 
	big_q.count().then(count => 
	{ 
		console.log("after big_q returns"); 
		if (count === 0) 
		{ 
			console.log("count is zero, no dups found, resolving the promise to the original 'item' so the insert can proceed");			 
			resolve(item); 
		} 
		else 
		{ 
			console.log("count is more than zero, rejecting the promise with an error message"); 
			reject("count is more than 0, dups found!"); 
		} 
	}); 
}); 

//return the promise from the hook 
return p; 

}

1 Like

All,

I’m having the exact same problem as mentioned above. Can anyone provide any insights on this? I’m beating head on the wall. :slight_smile: Basically, I’m trying to server up a coupon code from a pre-imported spreadsheet and each time the webpage is visited, I am marking a field to show that it was already used. Each time the site is visited, I query for the next item in the collection that doesn’t have a check mark in the “codeUsed” field, then I show the code and set the current item’s “codeUsed” flag to ‘true’ so it doesn’t get shown again. This works like a champ in the sand box and preview environment. However, with the live site, it doesn’t seem to be writing to the database to update the “codeUsed” field so it always shows the same code. Any ideas?? Here is the code:

import wixData from ‘wix-data’;
$w(“#CouponLabel1”).text = “”;
$w.onReady( function () {
$w(“#datasetAMZCodes1”).onReady( () => {
console.log(“The dataset is ready”);
wixData.query(“AMZ_CouponCodes_50off”)
.eq(“codeUsed”, false )
.find()
.then( (results) => {
if (results.items.length > 0) {
let item = results.items[0];
item.codeUsed = true ; // updated codeUsed boolean operator to track coupons that were distributed
wixData.update(“AMZ_CouponCodes_50off”, item);
$w(“#CouponLabel1”).text = item.promoCode;
} else {
console.log(wixData.query(“AMZ_CouponCodes_50off”));
//handle case where no matching items found
}
} )
. catch ( (err) => {
let errorMsg = err;
});

});
});

Please let me know if you have any ideas here! I appreciate it!

Just FYI - this thread is quite old, and not even connected to the issue that you’re having. Keep in mind that it’s better to post a new question as it will get more eyes on it and you’ll have a better chance of getting an answer.

With that in mind…

The second line of your code won’t work where you have it:
$w(" #CouponLabel1 ").text = “”;
It should be moved to be inside the $w.onRead() function.

The update() function uses the item’s ID. So you want something like this:
wixData.update(“AMZ_CouponCodes_50off”, item._id);

Good luck.

1 Like

Thank you Yisreal! I really appreciate the perspective. I will try this change today and post a new question next time around. Cheers!