[SOLVED] REST API cloud functions save only string type data

Hello.

I need to save numeric data into the database using cloud functions.

My problem is that if the column in the database is of type number I get the 400 response and data is not saved,

but if the column is of type string the operation is successful.

Yet I can’t keep it a string because I need to sort it. I tried converting the number to a string inside the cloud function, but it didn’t work.

    const Game = Moralis.Object.extend("Game");
    const game = new Game();
    const aiLevel = Number(request.params.aiLevel);
    game.set("aiLevel", aiLevel);

I’m using the REST GET requests to transfer the data to the cloud function and then save it to the DB from the cloud. My version of the react SDK is 1.3.5 and 1.5.9 of the moralis. I downgraded because of the strange errors in the new versions.

As a workaround, I will probably download all the data and then convert it to a number on the front end and sort it there instead of in the cloud. But this shouldn’t be so hard, what do you think?

Maybe that type Number is now what you expect it to be. Try to add some logging. It should be easy to save numbers for a column of type number.

1 Like

I’ve tested it with parseInt and Number, but I see that when the cloud function saves the data it saves it as strings as shown by the logger. So the output seems to be a string, even if the input is integer.

what is the type of data associated with that column?

if you hardcode a number it works fine?

It’s an integer. But since I transmit it in the URL via a GET request it becomes a string when it reaches the cloud function. That’s why I convert it back to the integer with the parseInt() inside the cloud function. Yet, it still doesn’t work.

It doesn’t work with a hardcoded number too

    const Game = Moralis.Object.extend("Game");
    const game = new Game();
    game.set("aiLevel", 7);
    await game.save();

I found another way of accomplishing what I need, but this issue exists.

there may be a different issue if it doesn’t work with a hardcoded value either, you could check the column type in the dashboard interface to be sure that it is not string

1 Like

I made a test. I ran the same code from cloud function and node.js. The code on node js successfully saves numbers to DB, while the cloud code doesn’t. Jfyi.

I don’t understand what would be the difference between running in cloud code and node js

I tried it again now from the cloud and it worked. Probably it wasn’t working because I was listening to the cloud changes from a folder different that the one I installed moralis cli to. So it wasn’t updating the function in the cloud when I was tweaking it. But I’m not sure about that. Anyway, it works now, thank you.

1 Like