Amount should be a number string

I have a event that receives this transaction


I’m trying to store event.amount to db.

when I try to store it in the table I get the error

“amount should be a number string”

which makes sense, since amount is a string but the column is number.

My question is, what is the best way to convert this to a number?

I know I can convert this to number in mongodb when I do the query, but I would rather not do that as that is more expensive.

you use event sync for this event, or you want to make you own custom insertion in db?

I would think that maybe it is better to let it as string, but you may have that overhead on converting it to a number at processing time, but at the same time you may have big numbers there and you may not want to have a conversion error when adding the number in db

at the moment it’s on sync event, but it will be a job reading from the sync event table.

I would really like to have the correct data type in the table, so if it’s possible to convert it before inserting, that would be my preferred option.

I’m also thinking, if the column is string and I want to do calculations on it in the cloud, I need to somehow convert it to number anyway.

I notice Morali.Units is undefined on the cloud

Doesn’t really help since they return string

let test = await Moralis.Cloud.units({
            method: "toWei",
            value: 1
    });"test:" + typeof(test)); //string

test = Moralis.Cloud.units({
            method: "fromWei",
            value: 1000000000000000
          });"test2:" + typeof(test)); //string

it’s simple :slight_smile: just use parseInt, I just didn’t expect it take such a big number

let number = parseInt(event.amount) // "34615251479801996" => 34615251479801996

for this number it may work, but for bigger numbers it may fail

Looks good for a very big number

it looks good, but there will be some digits lost in conversion, if you convert it back to string you may not get the same string from where you started

yes, and if I add one number to my original amount, I get wrong results. So no good :frowning:


do you have some other suggestion?