Calling a backend function results in "is not a function" error message

Hello all,

I am writing a velo code in wixStudio’s Cart Page that will populate item repeaters from my custom catalog. I have created an add and subtract buttons in the Cart Page, that will increment or decrement the quantity in the line items. To achieve that, I have created a backend function called changeItemQuantity. The backend function file getCart.web.js, that contains the function, looks like this:

import { Permissions, webMethod } from "wix-web-module";
import { currentCart } from "wix-ecom-backend";

export async function changeItemQuantity(lineItem) {
  const updatedLineItems = await currentCart.updateCurrentCartLineItemQuantity([lineItem]);
  return updatedLineItems;
}

I am calling this function inside an event listener, so that every time the addQuantityButton is clicked the quantity in the line items should increment by one. Below is the code where the changeItemQuantity gets called:

import {getCart, changeItemQuantity} from 'backend/getCart.web';


$w.onReady(function () {

	populateCartItems()
});


async function populateCartItems() {
	const cart = await getCart()
	const {lineItems} = cart
	console.log("Line Items=",lineItems)

	$w("#lineItemsRepeater").onItemReady(($item,itemData)=>{
		console.log("ItemData=",itemData)
		$item("#imageTitleText").text = itemData.productName.original
		$item("#printImage").src = itemData.image
		$item("#unitPriceText").text = itemData.price.formattedAmount
		$item("#quantityDisplayText").value = itemData.quantity.toString()
		$item("#printOptionsText").text = getProductOptions(itemData)

		$item("#addQuantityButton").onClick(async()=>{
			const {_id, quantity} = itemData;
			console.log("_id=",_id)
			const updatedQuantity = quantity + 1;
			console.log("Updated quantity=",updatedQuantity)
			const updatedLineItems = await changeItemQuantity({
				_id, 
				quantity: updatedQuantity,})
			console.log("updatedLineItems=", updatedLineItems)
			
		})
	});

	$w('#lineItemsRepeater').data = lineItems
}

function getProductOptions(product) {
	const options = product.catalogReference.options
	console.log("Options are = ",product.catalogReference.options)
	const keys = Object.keys(options)
	console.log("Keys =",keys)

	let optionsText = ""
	for (let i=0; i<keys.length; i++){
		optionsText = optionsText+`${keys[i]}: ${options[keys[i]]}\n`
	}
	console.log("OptionsText=",optionsText)
	return optionsText
}

However, upon publishing the website and using the + button in my cart, I get the following error message: "TypeError: (0,t.changeItemQuantity) is not a function. (In '(0,t.changeItemQuantity)({_id:o,quantity:a})', '(0,t.changeItemQuantity)' is undefined)"

Anyone has an insight what that might mean? The code runs normally until the line

const updatedLineItems = await changeItemQuantity({
				_id, 
				quantity: updatedQuantity,})

Your help will be very much appreciated.

1 Like

Hi, @user5208 !!

The cause of that error is likely that the changeItemQuantity function you defined does not match the format required by .web.js. I recently answered a similar question, so I’ll include a link for reference. :smiling_face_with_sunglasses: :raised_hands:

Hi @onemoretime , thank you for your help. Yes, the reason for the error was that the backend function wasn’t wrapped in a webMethod.

1 Like