How to run one cloud function in side of another one

how to run one cloud function in side of another one, also how to pass request from 1th function ?

Trying with

await Moralis.Cloud.run(“Foo”, {request:request})

but this does not work

Hi,

I know that you also asked on Discord. Did you make it work by now?

did you try with: await Moralis.Cloud.run(“Foo”, request)?

This also gives error

Uncaught (in promise) Error: Parse Objects not allowed here

can you past a simple example of two cloud functions where you try to do this?

Moralis.Cloud.define(“BalanceFun”, (request) => {
// do something with request
return 100;
});

Moralis.Cloud.define(“FirstFun”, (request) => {
const balance = await Moralis.Cloud.run(“BalanceFun”, request);

return balance * 10;
});

Moralis.Cloud.define(“SecondFun”, (request) => {
const balance = await Moralis.Cloud.run(“BalanceFun”, request);

return balance / 1000;
});

in js I run FirstFun and SecondFun that should run BalanceFun.

this seems to work:

Moralis.Cloud.define("BalanceFun", async (request) => {
// do something with request
return 100;
});

Moralis.Cloud.define("FirstFun", async (request) => {
const balance = await Moralis.Cloud.run("BalanceFun", JSON.stringify(request));

return balance * 10;
});

but it may not be what you want

I want to access logged user eth address

Moralis.Cloud.define(“BalanceFun”, async (request) => {
const userAddress = request.user.attributes.ethAddress;
return userAddress;
});

Moralis.Cloud.define(“FirstFun”, async (request) => {
const balance = await Moralis.Cloud.run(“BalanceFun”, JSON.stringify(request));

return balance;
});

getting this error now

moralis.js:26416 Uncaught (in promise) Error: Cannot read properties of undefined (reading ‘attributes’)

moralis.js:26416 Uncaught (in promise) Error: Cannot read properties of undefined (reading ‘attributes’)

Did you authenticate a user with Moralis.authenticate? Your BalanceFun function should work if you do.

Also please read this on how to post code.

you can use logger.info to see how the data looks like, it should be a string in this case

you can also use normal functions in cloud code, you don’t have to use cloud functions every time

Yes my user is logged in. Look here

Moralis.Cloud.define("BalanceFun", async (request) => {
//const userAddress = request.user.get('ethAddress');
//return userAddress;
return "";
});

Moralis.Cloud.define("FirstFun", async (request) => {
const user_fromFirstFun = request.user.get('ethAddress');
const user_fromBalanceFun = await Moralis.Cloud.run("BalanceFun", JSON.stringify(request));

return {'user_fromFirstFun': user_fromFirstFun, 'user_fromBalanceFun': user_fromBalanceFun};
});

{user_fromFirstFun: '0xXXXXXXXXXXX', user_fromBalanceFun: ''}

and now

Moralis.Cloud.define("BalanceFun", async (request) => {
const userAddress = request.user.get('ethAddress');
return userAddress;
});

Moralis.Cloud.define("FirstFun", async (request) => {
const user_fromFirstFun = request.user.get('ethAddress');
const user_fromBalanceFun = await Moralis.Cloud.run("BalanceFun", JSON.stringify(request));

return {'user_fromFirstFun': user_fromFirstFun, 'user_fromBalanceFun': user_fromBalanceFun};
});

moralis.js:26416 Uncaught (in promise) Error: Cannot read properties of undefined (reading 'get')

using simple javascript functions is not an option in this case?

also tried this

const Balance = async (request) => {
    return request.user.attributes.ethAddress;
};


Moralis.Cloud.define("FirstFun", async (request) => {
const user_fromFirstFun = request.user.get('ethAddress');
const bal = Balance(request);
return {'user_fromFirstFun': user_fromFirstFun, 'bal': bal};
});

But this time Balance returns {}

you can send directly the eth address to the other function

I dont want to send any address. Only logged in user should be able to read his data, not any other user data.

it seems different syntax here, you can add some logging to see if it works as expected

how to use simple javascript functions ? why example above is not working ?

the example above is simple javascript, try to add some logging

logger.info(JSON.stringify(variable_name)) usually works in cloud code for logging

Should be request.user.ethAddress

thank you. Works like this

const _Foo = async (request) => {
    return request.user.get('ethAddress');
};


Moralis.Cloud.define("Bar", async (request) => {
    const user = await _Foo(request)
    return user;
});

2 Likes