Set up Cloud Functions in your IDE Step 2: code example out of date with changes

Trying to sync cloud functions with local. After update to from 0.0.243 0.0.247.

moralis-admin-cli watch-cloud-folder --moralisApiKey --moralisApiSecret --moralisSubdomain
xxx.moralis.io --autoSave 1 --moralisCloudfolder /path/to/cloud/folder

console:

Invalid script watch-cloud-folder
The following are valid scripts:
watch-cloud-file
connect-local-devchain
update-server
create-server
add-contract
deploy
undeploy
help

This step text is not anymore up to date with recent changes?

The working example for me is now:

/src/moralis-cloud-functions$
moralis-admin-cli watch-cloud-file --moralisApiKey --moralisApiSecret --moralisSubdomain
xxx.usemoralis.com --autoSave 1 -p cloud.js 

1 Like

I already have a cloud_functions.js in my project am I suppose to create a new cloud file with this function ?

no this is just the moralis cli command that changed in a recent update, for if you use it to auto upload your local cloud file.

ahh got it. So otherwise I would just update cloud functions manually in the server.

Update the moralis-admin-cli

npm i -g [email protected]

Still struggling to make it work again. I’ve used cli for months with this:

moralis-admin-cli watch-cloud-folder --moralisApiKey xxx --moralisApiSecret xxx --moralisSubdomain xxx.moralis.io --autoSave 1 --moralisCloudfolder ./src/moralis-cloud-functions/cloud.js

But it doesn’t work anymore. I’ve now updated cli to [email protected] and with the same line I now get:

/bin/sh: 1: ncc: not found
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: Command failed: ncc build ./output.js -o dist
/bin/sh: 1: ncc: not found

node v16.3.

Using watch-cloud-file works sometimes, (cli always says ‘File Uploaded’) but most of the time does not update remote:

moralis-admin-cli watch-cloud-file --moralisApiKey xxx --moralisApiSecret xxx --moralisSubdomain xxx.moralis.io --autoSave 1 -p ./src/moralis-cloud-functions/cloud.js

Hey @matiyin

Regarding the watch-cloud-folder command:

/bin/sh: 1: ncc: not found
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: Command failed: ncc build ./output.js -o dist
/bin/sh: 1: ncc: not found

This is a known issue that we are fixing in the next release.
You can work around it by running the command npm i -g @vercel/ncc (use sudo if you are on Mac os Linux or open the cmd as administrator if you are on Windows).

Also the path is not correct as it should point to a folder not to a JS file.

An example:

1 - I have created an empty folder in my desktop called test-cloud-folder;

2 - I run the script watch-cloud-folder, let’s analyse the command:

moralis-admin-cli watch-cloud-folder --moralisApiKey NF0zYKHE639fANS --moralisApiSecret 4wV32YycpAos93j --moralisCloudfolder /Users/dani/Desktop/test_cloud_folder --moralisSubdomain vc3clw6eam3t.moralis.io --autoSave 1

Notice how the --moralisCloudfolder points to a folder /Users/dani/Desktop/test_cloud_folder.
The param --autoSave is set to 1 (true) which means that every time you will save a .JS file inside the folder specified, it will be pushed to cloud.

If I run the command above I get the same error as you:

Run npm i -g @vercel/ncc:

You are ready to go! :partying_face:

Run again moralis-admin-cli watch-cloud-folder --moralisApiKey NF0zYKHE639fANS --moralisApiSecret 4wV32YycpAos93j --moralisCloudfolder /Users/dani/Desktop/test_cloud_folder --moralisSubdomain vc3clw6eam3t.moralis.io --autoSave 1

and have fun, create a .js file in the folder you are pointing, write a cloud function in that file and save it, it will be uploaded to cloud automatically as soon as you save it!

Do not worry about all the other code that you see around you function, this is there to help you coding amazing stuff.

Why did we choose to add this watch-cloud-file script?
Because you can now split your cloud code in multiple folders and you can use node_modules.
We have some npm modules whitelisted that you can use in your cloud function.

Try the script out and keep us posted.

Happy coding,
Dani

2 Likes

hi @dani, much appreciated, I knew I didn’t go crazy after all :slight_smile:

I’ve got it working now, except the server UI doesn’t update after an upload, no loading icon and when you open Cloud Functions the changes are not there. You need to refresh the server UI page to see it.
But that’s ok now I know and understand you are working on this part.

Great news that you will allow more white listed modules! I asked about that previously here Which modules are whitelisted in cloud functions apart from node crypto lib?

Keep on buidling amazing stuff :heart_eyes:

1 Like

Happy to read the you solved, I will ping you here once the update to the script is released.

Keep on buidling amazing stuff :heart_eyes:

You can bet we will :muscle:

Hey @matiyin

The fix has been released!

Run npm i -g moralis-admin-cli, (sudo if mac/linux or run cmd as admin if windows).

Notice!
If you are on linux or mac os you will need to run the watch-cloud-folder command as sudo, the rest remains the same:

sudo moralis-admin-cli watch-cloud-folder --moralisApiKey NF0z23Eewfew32A --moralisApiSecret 23i9djm2xrxr49n3 --moralisCloudfolder /Users/dani/Desktop/test_cloud_folder --moralisSubdomain vc35464ljzx.moralis.io --autoSave 1

Keep me posted!

@dani cool, I removed vercel/ncc and updated the cli. I’m on linux and what I found:

sudo moralis-admin-cli watch-cloud-folder --moralisApiKey xxx --moralisApiSecret xxx --moralisSubdomain xxx.usemoralis.com --autoSave 1 --moralisCloudfolder ./src/moralis-cloud-functions/

sudo: moralis-admin-cli: command not found

I elevated my user with ‘su’ and that works:

ncc: Version 0.29.1
ncc: Compiling file index.js into CJS
Changes Uploaded Correctly

File is update on remote, although you still have to refresh the browser to see the change.

I’m not sure why sudo doesn’t work here, never had that before with anything!

1 Like

sudo moralis-admin-cli watch-cloud-folder --moralisApiKey xxx --moralisApiSecret xxx --moralisSubdomain xxx.usemoralis.com --autoSave 1 --moralisCloudfolder ./src/moralis-cloud-functions/
sudo: moralis-admin-cli: command not found

This means that the cli is not installed in your machine.

Run sudo npm i -g moralis-admin-cli

The run the command again.

I can replicate it by uninstalling the cli:

Install CLI:
Screenshot 2021-08-13 at 17.00.02

Run the command again:

Regarding the browser update, which one are you using? I do see real time changes in my cloud code without updating it.

funny, i guess i needed to restart the terminal, it works now without ‘sudo’, which sudo i still get

sudo: moralis-admin-cli: command not found

@update: I installed now also using sudo and all is fine, so it works with and without sudo now for me.
I do get his warning when installing with sudo:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected]~2.3.2 (node_modules/moralis-admin-cli/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

I’m on Brave
Version 1.27.109 Chromium: 92.0.4515.115 (Official Build) (64-bit)

with all Shields down and websockets not connecting:

I can confirm Firefox works indeed!
Brave used to work fine until some weeks ago.

@dani something else that I sometimes encounter using cloud functions, synced with local:

This line (very occasionally) suddenly appears in the top of my local cloud.js file:

const { Moralis } = require("app/../../../../../media/matiyin/Data/git/qmarket/node_modules/moralis/lib/browser/Parse");

I notice because it stops my cloud functions from working, throwing an error in your server UI, the red triangle.
When I remove the line all is fine again. Sometimes takes me 10 minutes to find out hahaha.

Could it be from my local setup or can the cloud sync really write in my file? :thinking:

I guess the UI throws an error because of the require statement, would be nice to know which code you wrote to verify what’s triggering the creation of that line

@dani I got it again, and it was after I added some (bad) pipeline code add saved. Very strange why it suddenly inserts a Parse lib. I’ll keep an eye on it, it’s not a showstopper but just annoying because sometimes you don’t realize that this line in there and breaks code.

  const pipeline = [
    // {
    //   match: {
    //     contract_type: 'ERC721',
    //     token_address: request.params.token_address,
    //     owner_of: { $in: request.params.forUserAddress ? [request.params.forUserAddress] : request.user.attributes.accounts },
    //   }, 
    // },
    {
      lookup: {
        from: request.params.network+'ItemsForSale',
        localField: "token_id",
        foreignField: "token_id",
        as: "marketItem"
      }
    },
    {
      lookup: {
        from: Moralis.User,
        localField: "accounts",
        foreignField: "owner_of",
        as: "OwnerItem"
      }
    },
    { sort : { block_number: -1 } },
  ]
  const marketItems = query.aggregate(pipeline)
  return marketItems

ok I got it :slight_smile:

I’m trying is use a User class in a pipeline lookup:

    {
      lookup: {
        from: "_User",
        localField: "owner_of",
        foreignField: "ethAddress",
        as: "ownerItem"
      }
    },

While this works, the problem here is that it returns only a filename for the field ‘avatar’ and I cannot call the .url() to get the actual path to the file:

ownerItem: {
  accounts: ["0xxxx"],
  avatar: "ca14609a305754e1d7cf48ff07f1f3c3_myavatar.png",
  ethAddress: "0xxxx",
  username: "Master",
  etc,
}

So I tried to use:

    {
      lookup: {
        from: Moralis.User,
        localField: "owner_of",
        foreignField: "ethAddress",
        as: "ownerItem"
      }
    },

which on saving adds:

const { Moralis } = require("app/../../../../../media/matiyin/Data/git/qmarket/node_modules/moralis/lib/browser/Parse");

Obviously I’m doing something wrong here and lookup can only be a string, but at least I know what causes this issue!
Still wondering how I can get the avatar.url() in this case though :slight_smile: