Passing a variable to a backend page

I have hacked together a solution to provide a unique serial key and password to a user who purchases a product. I have some backend code that I am using to complete a transaction, and it is getting the name and price of the product from a wixData.query (below is the code from the pay.js backend page)

export async function createMyPayment(productId) {
 return wixData.query("I need to pass a variable here")
        .find()
        .then((product) => {
 let paymentInfo = ({
                items: [{
                    name: 'The Cool Product:<br>  Serial Key:<br> ' + product.items[0].serialKey + '<br>Password:<br>' + product.items[0].password,
                    price: 1
                }],
                amount: 1
            });
 return wixPay.createPayment(paymentInfo);
        });

}

What I need to do now is pass a variable from the front end page to this back end page to tell it which database to query …

Can someone help?
@Certified Code - Velo Certified ?

Back-End-Code:

export async function createMyPayment(productId, VARIABLE) {
    return wixData.query(VARIABLE)
    .find()
    .then((product) => {
 let paymentInfo = ({
            items: [{
                name: 'The Cool Product:<br>  Serial Key:<br> ' + product.items[0].serialKey + '<br>Password:<br>' + product.items[0].password,
                price: 1
            }],
        amount: 1
        });
    return wixPay.createPayment(paymentInfo);
    });
}

Front-End-Code:

//Front-End-Code
import {createMyPayment} from 'backend/nameOfBackEndModul.JSWhere'

$w.onReady(()=>{
 let VARIABLE = "CollectionNameHere"
    createMyPayment(VARIABLE)
})

But you should ask your question in the VELO-FORUM instead here.

I will be sure to post questions like this in the Velo Forum from now on; thank you for pointing that out …

I’m looking forward to trying to implement your solution, I will let you know how it goes …

I’m getting an error, it is saying that the variable on the .jsw page is undefined :frowning:

Failed to perform query on [undefined]

@burkedunnweb
Try this one…

//Front-End-Codeimport{createMyPayment}from'backend/nameOfBackEndModul.JSWhere'  $w.onReady(()=>{
    letVARIABLE="CollectionNameHere"createMyPayment(productId, VARIABLE)
})

@russian-dima I actually did, and I get the error that productId is undefined

@russian-dima HOWEVER … if I put this in the front end code:

let productId

… then it APPEARS to work … !!!

So it WAS the solution (or so it seems) … thanks

@burkedunnweb
Of course it will just work if you also provide an productId.
You are calling a function called → createMyPayment ( productId, VARIABLE )
which is in your Back-End (from your Front-End).

This function needs 2 values —> productId, VARIABLE.

That means, if you call it like this…

createMyPayment(VARIABLE)

…you provide just one of the 2 needed VARIABLES to the Back-End (but the function need 2!!!)

That means, this one would be the right one…

createMyPayment(productId, VARIABLE)

You put in a —> productId & a —> VARIABLE into it.

let VARIABLE = "XYZ" 		//<---- what ever
let productId = "9983483" 	//<---whatever

createMyPayment(productId, VARIABLE)

Back-End accept the values…

export async function createMyPayment(productId, VARIABLE) {
    return wixData.query(VARIABLE)
    .find()
    .then((product) => {
 let paymentInfo = ({
            items: [{
                name: 'The Cool Product:<br>  Serial Key:<br> ' + product.items[0].serialKey + '<br>Password:<br>' + product.items[0].password,
                price: 1
            }],
        amount: 1
        });
    return wixPay.createPayment(paymentInfo);
    });
}

But when i take a closer look onto your Back-End-Code, it seems that something is missing???

And after some following seconds i recognized your wished scenario.

What you need is …

  1. The name of the collection as a —> VARIABLE
  2. A second VARIABLE —> “productId”
    …to pass them to Back-End. I think this is all you wanted to do, like…
export async function createMyPayment(productId, VARIABLE) {
    return wixData.query(VARIABLE)
    .eq("FIELD",productId) //<---"FIELD" = the right COLUMN in your DATABASE
    .find()
    .then((product) => {console.log(product)
         let paymentInfo = ({
            items: [{
                name: 'The Cool Product:<br>  Serial Key:<br> ' + product.items[0].serialKey + '<br>Password:<br>' + product.items[0].password,
                price: 1
            }],
        amount: 1
        });
    return wixPay.createPayment(paymentInfo);
    });
}

You could also provide the FIELD, and tell your CODE in which COLUM in your DB it should do the search.

Front-End:

let VARIABLE = "myCollection" 	    //<---- Collection-Name / DB-Name/ID
let productId = "9983483" 	    //<---whatever
let FIELD = "dataField"             //<--- the right COLUMN in DB

createMyPayment(productId, VARIABLE, FIELD)

Back-End:

export async function createMyPayment(productId, VARIABLE, FIELD) {
    return wixData.query(VARIABLE)
    .eq("FIELD",productId) //<---"FIELD" = the right COLUMN in your DATABASE
    .find()
    .then((product) => {console.log(product)
         let paymentInfo = ({
            items: [{
                name: 'The Cool Product:<br>  Serial Key:<br> ' + product.items[0].serialKey + '<br>Password:<br>' + product.items[0].password,
                price: 1
            }],
        amount: 1
        });
    return wixPay.createPayment(paymentInfo);
    });
}

You can see the resulting OUTPUT in CONSOLE!
Press F-12 in google-chrome-browser and navigate to → CONSOLE!

I hope, it’s a little bit clearer now for you. It is not recommended to use some code by copy&paste, but do not understand it :grin::roll_eyes:

@russian-dima Thank you so much man … while that didn’t work at first (I was getting an error that productId was undefined), it did when I just declared productiD without a value at the beginning of the onReady function …

I’m going to delete this thread soon, since I created a duplicate in the Velo forum …