MongoDB Database Values

Hi,

i have quite an interesting questions about the Database.
Lets take the “BscTokenTransfer” class as an example.

Is it fixed how it fills? (as it fills automatically as i can see)… or is it changeable?

To be specific… i would like to have the USD value of that buy/sell/transfer in that database.

Is that possible anyhow?

thx for the help
greetings

You can use an afterSave hook and add that value in a new column. Or you can use a job that runs every x minutes that does that processing.

The initial data is fixed, you can use hooks to add more info. Don’t try to modify current columns.

1 Like

yeah i just found that out… damn moralis is really one of the best things for the cryptoworld!

Thx for the help, definitly will use that :slight_smile:

1 Like

hi, is there anything i have to be aware of if i add a custom column? i added one to BscTokenTransfers, but if i do a query for example the column is not shown. and if i try to update it, its not updated.

How did you add it? What you mean by is now shown?

You have to add it in dashboard interface

so i went to the dashboard, selected “BscTokenTransfers” clicked on “add a new column” type string, name usdval, and finally added it.

Did also restart the server, just to be sure.

So once the cloudfunction is running, i am doing some debug. first here the current coding just using for testing the functions to learn:

Moralis.Cloud.afterSave("BscTokenTransfers", async function (request) {
  const confirmed = request.object.get("confirmed");
  if (confirmed) {
    		const logger = Moralis.Cloud.getLogger();
    		logger.info(JSON.stringify(request.object));
    	    logger.info("Token "+request.object.get("token_address")); 
    		logger.info("From "+request.object.get("from_address")); 
    		logger.info("Block "+request.object.get("block_number")); 
    	 
    		
    		if(request.object.get("from_address") != "0x0000000000000000000000000000000000000000")
            {
              	const options = {
                    address: request.object.get("token_address"),
                    chain: "bsc",
                    to_block: request.object.get("block_number"),
                  };
              
              	logger.info(options); 
                const price = await Moralis.Web3API.token.getTokenPrice(options);
              
              	logger.info("price "+price.usdPrice); 
              
              	
    
				const BscTransfers = Moralis.Object.extend("BscTokenTransfers");
                const query = new Moralis.Query(BscTransfers);

                
                query.equalTo("transaction_hash", request.object.get("transaction_hash"));

                const result = await query.first({useMasterKey:true})
                logger.info(JSON.stringify(result));
                result.set("usdval",price.usdPrice);
                result.save();
			
              
               
            }
    
  } else {
    	
   	
  }
});



The logger infos are working, i get all the datas i need from that request.
But the update of the column does not work (no error also, wich i dont understand)

and thats the log i get from the JSON.stringify(result)


{"from_address":"0x0000000000000000000000000000000000000000","log_index":146,"to_address":"0xa81fc1c50.......","transaction_hash":"0x4b02a30240cc0a5a33.......","createdAt":"2022-04-14T18:16:33.014Z","updatedAt":"2022-04-14T18:17:44.188Z","block_hash":"0x19d873e4740515e4aaf6........","block_number":16945665,"block_timestamp":{"__type":"Date","iso":"2022-04-14T18:16:32.000Z"},"decimal":{"__type":"NumberDecimal","value":"766688888.086156601507948247"},"token_address":"0xc0908ea0b6642a.....","transaction_index":77,"value":"766688888086156601507948247","confirmed":true,"objectId":"WK0l9vrKTmE2HYv4qSkv4v74"}

as you can see, the “usdval” column is not listed … idk if it lists it because its “(undefined)” at the moment

but either way, the function runs through, and the value is still undefined

Try await result.save(null, {useMasterKey:true})

1 Like

that worked perfectly! thank you very much!.
btw is there an easy way to clean the database ? just want to reload everything :slight_smile:

I don’t know of an easy way to do that. You can delete the data from a table directly in the dashboard.

ok thx. one last question… as i see the transaction_hash is not uniqe. Is there any way to get access to the objectId from the request? or is there anything unique i can use to query the right row?

and i want to rent a production server, is there any quick way to contact the support for a payment link?

For a production server you have to send an email to [email protected]

Not sure what you mean by from the request

Try .id if you have the result from a query

Moralis.Cloud.afterSave("BscTokenTransfers", async function (request) {

that part the “request” id does not contain the objectId of the database of course… but i need like something unique to query the database … as the transaction_hash is not unique (for example if a dividend-tracker is triggered…) … if not i just update all the same txhashes ^^

thx for the email, i will send them email now :slight_smile:

You have the id there in that request parameter. You can update directly the object from that request.

You can see what is in that request with logger.info(JSON.stringify(request))

yeah i know that, but the objectID is always undefined, even if i can see it in the logger

it should be

request.object.get("objectId");

but its undefined

did you try object.get("id") ?

yeah then i get SyntaxError: Unexpected identifier

what did you see in the request object with logger.info?

also, make sure that you have latest version of moralis server

this should work request.object.id

{
   "triggerName":"afterSave",
   "object":{
      "token_address":"0x651a89fed302227d4142........",
      "log_index":220,
      "transaction_hash":"0x4aa1b4cb52bb5.............",
      "transaction_index":57,
      "block_timestamp":{
         "__type":"Date",
         "iso":"2022-04-14T20:16:50.000Z"
      },
      "block_number":16948071,
      "from_address":"0x504d67fe989eed2087...........",
      "to_address":"0xa81fc1c5062435d2..............",
      "value":"248930782282220820357407519",
      "block_hash":"0xe56f5d99c82dc4375a3de5af93be91d3...............",
      "decimal":{
         "__type":"NumberDecimal",
         "value":"248930782.282220820357407519"
      },
      "updatedAt":"2022-04-14T20:16:51.305Z",
      "objectId":"SaoF3h20XVfgnbWaSoVH02OH"
   },
   "master":false,
   "log":{
      "options":{
         "jsonLogs":false,
         "logsFolder":"./logs",
         "verbose":false,
         "maxLogFiles":1
      },
      "appId":"HWesQViW..............."
   },
   "headers":{
      "accept":"application/json, text/plain, */*",
      "content-type":"application/json",
      "x-parse-application-id":"....W.......",
      "x-parse-session-token":"r:c332b4afca87a2ac......",
      "user-agent":"axios/0.21.4",
      "content-length":"840",
      "host":"my-moralis-server:1337",
      "connection":"close"
   },
   "ip":"xxx.xxx.xxx.xxx",
   "original":{
      "objectId":"SaoF3h20XVfgnbWaSoVH02OH"
   },
   "context":{
      
   },
   "user":{
      "username":"coreservices",
      "createdAt":"2022-04-02T15:21:39.990Z",
      "updatedAt":"2022-04-02T15:21:39.990Z",
      "ACL":{
         "role:coreservices":{
            "read":true,
            "write":true
         },
         "VET.........hUoOyG":{
            "read":true,
            "write":true
         }
      },
      "sessionToken":"r:c332b4a........d707d08",
      "objectId":"VETB........cYmjOhUoOyG"
   }
}

this is btw the right objectId: SaoF3h20XVfgnbWaSoVH02OH

as you can see, its there… but i get undefined everytime ^^

the server is version 0.0.369

read the above reply, I edited it