queryCollection - Exception Occurred When Attempting to Run Script


#1

Hi,
I have problem with queryCollection from this page:
https://docs.chilliconnect.com/guide/tutorial-marketplace/
I’m geting the

HTTP/1.1 422

{
“Error”: “Exception Occurred When Attempting to Run Script”,
“Code”: 9002
}

I created the required collection

My code looks like this:

//try {
var sdk = ChilliConnect.getSdk("2.18.0");

//Extract ItemId from Request
    var itemID = ChilliConnect.Request.ItemID;

//Verify that the Item exists in the player's inventory
var items = sdk.Economy.getInventoryForItemIds([itemID]);
if ( items.Items.length === 0 ) {
    return { "Error" : "ItemID does not exist" }
}

//Verify the item has not been put up for sale already
var existingSale = sdk.CloudData.queryCollection( "SALE_ITEMS", 
    "Value.ItemID = :ItemID", [], {"ItemID" : itemID } );

if ( existingSale.Total > 0 ) {
    return { "Error" : "ItemID already for sale" }
}

//Get the Item
var item = items.Items[0];

//Extract Price from request and ensure it's greater than 0
var price = parseInt(ChilliConnect.Request.Price);
if ( price <= 0 ) {
    return { "Error" : "Invalid price" }
}

//Limit the description to a max of 50 characters
var description = ChilliConnect.Request.Description.substring(0, 50);

//Add the item to the sale items collection
var saleItem = sdk.CloudData.addCollectionObject( "SALE_ITEMS", {
    "ItemID" : itemID,
    "Name" : item.Name,
    "Price" : price,
    "Description" : description,
    "Completed": false
});

//Return the ID of the item to the client
return { "SaleItemID" : saleItem.ObjectID };

//}
//catch(e)
//{
//    ChilliConnect.Logger.error( e.toString() );
//
//    return { "Error" : "Unknown Error" };
//}

Could someone tell me what is wrong?


#2

Hi,
Found the problem.
The query should look like this:
var existingSale = sdk.CloudData.queryCollection("SALE_ITEMS", "Value.ItemID = :itemID", {"itemID" : itemID});


#3

Good spot :slight_smile:

A debugging tip is to wrap code producing the “Exception Occurred When Attempting to Run Script” in a try/catch and log the exception which will give more information to work on…

try {
    var existingSale = sdk.CloudData.queryCollection( "SALE_ITEMS", "Value.ItemID = :ItemID", [], {"ItemID" : itemID } );
}
catch(e)
{
    ChilliConnect.Logger.error("E: " + e);
}

Logs:

“E: 1007: Invalid Request {“SortFields”:[“The sort fields field is invalid.”]}”