How to call Localhost API from Moralis cloud httpRequest

Is anyone know how to call localhost API from Moralis cloud httpRequest?

here is my code.
Moralis.Cloud.afterSave(‘EthTransactions’, (request) => {
const logger = Moralis.Cloud.getLogger();
// Take the transaction and call the API to save the data into mongodb.
const transaction = request.object;
const user_address = transaction.get(‘from_address’);
const chain = ‘eth’;
const block_hash = transaction.get(‘block_hash’);
const gas_price = transaction.get(‘gas_price’);
const block_timestamp = transaction.get(‘block_timestamp’);
const receipt_cumulative_gas_used = transaction.get(‘receipt_cumulative_gas_used’);
const ACL = transaction.get(‘ACL’);
const receipt_gas_used = transaction.get(‘receipt_gas_used’);
const input = transaction.get(‘input’);
const hash = transaction.get(‘hash’);
const updatedAt = transaction.get(‘updatedAt’);
const nonce = transaction.get(‘nonce’);
const to_address = transaction.get(‘to_address’);
const transaction_index = transaction.get(‘transaction_index’);
const value = transaction.get(‘value’);
const decimal = transaction.get(‘decimal’);
const gas = transaction.get(‘gas’);
const receipt_status = transaction.get(‘receipt_status’);
const createdAt = transaction.get(‘createdAt’);
const block_number = transaction.get(‘block_number’);
const from_address = transaction.get(‘from_address’);
const confirmed = transaction.get(‘confirmed’);

const body = {
    user_address,
    chain,
    block_hash,
    gas_price,
    block_timestamp,
    receipt_cumulative_gas_used,
    ACL,
    receipt_gas_used,
    input,
    hash,
    updatedAt,
    nonce,
    to_address,
    transaction_index,
    value,
    decimal,
    gas,
    receipt_status,
    createdAt,
    block_number,
    from_address,
    confirmed
}

logger.info("body::: " + body);

Moralis.Cloud.httpRequest({
    method: 'POST',
    url: 'http://localhost:8080/api/save/transaction',
    body: body,
}).then(function (httpResponse) {
    logger.info(httpResponse.text);
}, function (httpResponse) {
    logger.error('Request failed with response code ' + httpResponse.status);
});

})

hello, please check here on how to post code on forum:

you can not use localhost in url: 'http://localhost:8080/api/save/transaction', you have to use a public IP there and has to be accessible over the internet

Okay, Then how can we copy all data of Moralis DB into mongo DB?
Like I want to copy all transactions whenever a new transaction is being held.

you can access directly the mongo db database that your Moralis Server uses:
https://docs.moralis.io/moralis-dapp/database/direct_access

1 Like

Hey @cryptokid,
I have created a db.js file and successfully connected with Moralis db using the below code.
But now I am confused about how can we find all data from an EthTransaction ?

const { MongoClient } = require('mongodb');
const client = new MongoClient(`mongodb://${MONGO_HOST}:${MONGO_PORT}`)

module.exports = {

	connectToServer: async function () {
		try {
			console.log("testing..", client)
			await client.connect();
			await client.db('admin').command({ ping: 1 });
			console.log('Connected to database: ' + 'admin');
		} finally {
			client.close();
		}

	},
}

In the index.js file, I have written this code to find the EthTransactions.

app.get('/api/test', async function (req, res) {
    console.log("hittt..")

    const query = new Moralis.Query("EthTransactions");
    query.descending("updatedAt");
    const results = await query.find();
    console.log("results..", results)
});

But the question is that I could not able to access directly. It is based on Moralis Query not Mongo Query.
And I need to find with Mongo Query to find all transactions from EthTransaction.

You will find everything in parse db when you connect directly to mongodb

Check the python example from documentation

@cryptokid Could you please provide me Nodejs example where the same thing is written. Because on Moralis docs they did not mentioned properly.

Try to look at the python example to understand what it does. You can also list the databases and tables directly from code.

Okay, And db name should be ‘parse’ or something else?

Yes, parse is the name of the db where you can find the data you want. There are multiple dbs there.

1 Like

Thanks man!! It’s working!!

1 Like