Calling a cloud function using API when param includes a number

Hi guys, great work with the forum!

Iโ€™m trying to call a cloud function via REST API which requires two parameters.
When I call the function I am getting no results because the server is converting my number parameter to a string in the Input object

Example 1
If I run the cloud function from front end of app like this:

await Moralis.Cloud.run("testFunction",{name:"MP", number:5})

The logs display:

Input: {"name":"MP","number":5}
Result: [Loads of results, working normally]

Example 2 -
If I convert my cloud function into an API call and have something like this:

fetch('MYSERVER/server/functions/testFunction?_ApplicationId=MYAPPID&name=MP&number=5')

The logs display:

Input: "_ApplicationId":"MYAPPID","name":"MP","number":"5"}
Result: []

I notice that the function returns nothing because the number param is going through as a string and not a number.

Is there a simple way around that?

you could convert it back to a number in that cloud function

I experimented with the field validation, but it errors because the number field is not a number, itโ€™s going through as a string

Moralis.Cloud.define("testFunction", async (request) => {
  
  const unitQuery = new Moralis.Query("Unit");
  unitQuery.greaterThan(request.params.name, request.params.number);
  const tokenResult = await unitQuery.find();
  const results = tokenResult.map((token) => token.attributes);
  
  return results;
},{
  fields : {
    name : {
      required: true,
      options: name => {
        return name.length > 1;
      },
      error: "Test error message"
    },
    number : {
      type: Number,
    }
  }
});
fetch('https://aegk8vmdrtol.bigmoralis.com:2053/server/functions/testFunction?_ApplicationId=YT8QJ8naY8HxsO2hfK10mrWsjPH5ZJJtliq1DG3e&name=MP&number=5').then(so on and so on)

then you can send it as string in both cases, and validate it as string, or use only one of those two methods of calling the cloud function

Ok yep, both issues can be marked as resolved

Iโ€™ll add some conditionals in the cloud function to convert my params to the data type I need
Iโ€™ll edit my validation

Thanks again
(world class support)

1 Like