You cannot use [object Object] as a query parameter

Hey. I’m trying to create a cloud function, but have ran into an issue - it throws Error: You cannot use [object Object] as a query parameter.. If anyone can help, that would be great. Thanks in advance.

Moralis.Cloud.define("returnBalanceOfUser", async (userid,contractAddress,BscChain) => {
   const query = new Moralis.Query("User");
    query.equalTo("userid", userid);
    const results = await query.find({useMasterKey:true});
    var bal = 0;
    if(results.length > 0){
        for (let i = 0; i < results.length; i++) {
            const object = results[i];
            const options = { chain: BscChain, address: object.get("ethAddress")}
            const balances = await Moralis.Web3API.account.getTokenBalances(options);
            const parsed = JSON.parse(balances);
                
            for(var j = 0; j < parsed.length; j++){
                if(parsed[j]["token_address"] == contractAddress){
                    bal += parsed[j]["balance"];
                    break;
                }

            }
        }
    }
    else bal = -1;
    return bal;
});
1 Like

Hey @Pantk

Try to specify:

const parsed = JSON.parse(balances.result);

Hello Yomo,
Me too i have the same problem on my Cloud function. My browser consoles states this in return :

moralis.js:25496 POST https://XXXXXXXXXXXXX.usemoralis.com:2053/server/functions/searchItems_transaction_hash 400
dispatch @ moralis.js:25496
ajax @ moralis.js:25503
(anonymous) @ moralis.js:25610
Promise.then (async)
request @ moralis.js:25604
run @ moralis.js:430
run @ moralis.js:356
searchItems @ main.js:478
onclick @ index.html:86
moralis.js:28290 Uncaught (in promise) Error: You cannot use [object Object] as a query parameter.
    at handleError (moralis.js:25632:17)
    at async searchItems (main.js:478:21)

This my cloud function:

Moralis.Cloud.define(“searchItems_transaction_hash”, async (whichKeyword) => {

const query = new Moralis.Query(“ItemsForSale”);
query.equalTo(“transaction_hash”, whichKeyword);

const queryResults = await query.find();

if(queryResults){
return {
“askingPrice”: queryResult.attributes.askingPrice,
“block_timestamp”: queryResult.attributes.block_timestamp,
};
}

});

This function is called by this on the frontend:

onclick="searchItems(document.getElementById('search_engine_ipt').value);"

… and this is the function in the frontend folder:

searchItems = async (whichKeyword) => {
  const TRX_found = await Moralis.Cloud.run("searchItems_transaction_hash", whichKeyword);
  if(TRX_found){
    alert("TRX found!");
 }
}

Note:
1- I still under Web3.js
2- why 2 functions searchItems() + searchItems_transaction_hash() ?
it’s because searchItems() will be a global search function hosting other search functions for precise cases ; like searchItems_transaction_hash() is only to search transactions number and render the proper HTML node.

you could log the value for whichKeyword and see if it has the expected value

you can also try to run this query separately with a hardcoded value and with a variable to see if it works

1 Like

On the client side, when launching the client-side script, i noticed no error. I can alert() and console.log() and i get the expected value returned:
submitting “0x879b869825cb7462b15025ce5b6b7943e0292fd5943f82387aac32091919a0a7” from the frontend and i get in console the same data returned. I checked typeof() and it returns “string” (and not “[object Object]” as it is now returned back from the cloud).

So, i tried to do the same in the cloud function. Below, in bold, what i added to checkout the expected argument:

Moralis.Cloud.define(“searchItems_transaction_hash”, async (whichKeyword) => {
const query = new Moralis.Query(“ItemsForSale”);
query.equalTo(“transaction_hash”, whichKeyword);

const queryResults = await query.find();

console.log(whichKeyword);
** if(queryResults){**
** return "OK: " + whichKeyword;**
** }else{**
** return "NOT OK: " + whichKeyword;**
** }**

});

When the above cloud script is on, this is what i get in return in local console :

I have the feeling that when i send a string as argument to the cloud function, the cloud function returns an object with several properties instead of a string, right? As if the cloud function fell on a cell linked to another database-table cell.

about, my screenshot, if i click in the error message on “moralis.js:28290”, i can see that the Moralis SDK has issue :
return _construct.apply(null, arguments);

I use this SDK:
<script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>
Maybe it’s too old ?

I don’t think that it is from SDK, you can add more logging in that cloud function to see on what line it fails

1 Like

[I update this poste as the issue remains]
I have found another way to declare my function to stop with console error. Thanx to Ivan on Tech!!
I was trying to use the local script below to talk to my cloud function, but it does not work (Error 400 constantly returned):

searchItems = async (whichKeyword) => {
  // blabla
}

I discovered that this syntax below is working great :

async function searchItems(whichKeyword){  
  // Blabla
}

it was mentionned here : Moralis Cloud Functions Explained - Hello World, Multiple Files and IDE Setup

I have a new issue: I cannot send an argument to a cloud function. Is it correct ?
I want to send a transaction number to my cloud function like that :
`searchItems(whichKeyword);

Is it possible to send an argument to the cloud function and get a response depending what is found with my argument? Possible ?`

Yes, it is possible. Check the subsection “Calling Cloud Functions” on the below documentation page. The code will look like this -

const params =  { movie: "The Matrix" }; //the parameter you want to send. In your case whichkeyword
const ratings = await Moralis.Cloud.run("averageStars", params); //calling your cloud function

https://docs.moralis.io/moralis-server/cloud-code/cloud-functions

1 Like

Hello malik !
Thank you very much: YES, that is THE solution ! I’ve readen the full article + watched Ivan’s video on Cloud function. It looks amazing on my side and works great. No error now back in the client.
Do you know how how i can concatenate several arguments? With “,” or “;” ? Example:
const params = { movie: "The Matrix", date: "1999"};

1 Like

Yes, thats how you do it.

And to access it in the cloud function, you can do –

request.params.movie
request.params.date
1 Like

Thank you, Malik!
Oh là là là là!!! I think i’m going to have a lot of fun with this Moralis stuff :wink:

1 Like