Hi,
I’m watching moralis with great interest and having fun following a fun clone project.
While following Morarible content, I ran into the following problem, but it is not easy to solve.
1. Error Message of dashboard when I was trying to run cloud functions.
I can’t find the reason why the following issue occurs, and the bigger problem is that it is difficult to debug cloud functions. Can you help me why is this happening?
And are there good ways to solve problems in cloud functions?
2021-07-30T07:32:59.421Z - Error: Invalid function: "getUserItems"
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (internal/process/task_queues.js:95:5)
2021-07-30T07:32:52.364Z - SyntaxError: Unexpected identifier
at customUserPlugin (/moralis-server/cloud/main.js:15:26)
at /moralis-server/lib/cloud-code/plugins/index.js:62:15
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:51:3)
2021-07-30T07:32:52.363Z - CLOUD FUNCTION ERROR PLEASE CHECK YOUR CLOUD FUNCTION CODE
2. Cloud Functions Full Code for Morarible
Moralis.Cloud.define("getUserItems", async (request) => {
const query = new Moralis.Query("EthNFTOwners");
query.equalTo("contract_type", "ERC721");
query.containedIn("owner_of", request.user.attributes.accounts);
const queryResults = await query.find();
const results = [];
for (let i = 0; i < queryResults.length; i++) {
results.push({
"id": queryResults[i].attributes.objectId,
"tokenId": queryResults[i].attributes.token_id,
"tokenAddress": queryResults[i].attributes.token_address,
"symbol": queryResults[i].attributes.symbol,
"tokenUri": queryResults[i].attributes.token_uri
});
}
return results;
});
Moralis.Cloud.beforeSave("ItemsForSale", async (request) => {
const query = new Moralis.Query("EthNFTOwners");
query.equalTo("token_address", request.object.get("tokenAddress"));
query.equalTo("token_id", request.object.get("tokenId"));
const object = await query.first();
if (object) {
const owner = object.attributes.owner_of;
const userQuery = new Moralis.Query(Moralis.User);
userQuery.equalTo("accounts", owner));
const userObject = await userQuery.first({useMasterKey:true});
if (userObject) {
request.object.set('user', userObject);
}
request.object.set('token', object);
}
});
Moralis.Cloud.beforeSave("SoldItems", async (request) => {
const query = new Moralis.Query("ItemsForSale");
query.equalTo("uid", request.object.get('uid'));
const item = await query.first();
if(item){
request.object.set('item', item);
item.set('isSold', true);
await item.save();
const userQuery = new Moralis.Query(Moralis.User);
userQuery.equalTo("accounts", request.object.get('buyer'));
const userObject = await userQuery.first({useMasterKey:true});
if(userObject){
request.object.set('user', userObject)
}
}
});