[SOLVED] Deploy cloud functions

back then, there’s a web IDE where code can be pasted into but now everything has to be running on local IDE with moralis-admin-cli and I was wondering if there’s a way to deploy these functions? I keep getting "Invalid function: \"loadNFTs\" and below is my code

Moralis.Cloud.define('loadNFTs', async (request) => {
  const query = new Moralis.Query('NFT');

  // TODO refactor nicely
  if (request.params.hasOwnProperty('artistId')) {
    query.equalTo('artistId', request.params.artistId);
  }

  if (request.params.hasOwnProperty('songId')) {
    query.equalTo('songId', request.params.songId);
  }

  if (request.params.hasOwnProperty('rarity')) {
    query.equalTo('rarity', request.params.rarity);
  }

  if (request.params.hasOwnProperty('featured')) {
    query.equalTo('featured', request.params.featured);
  }

  if (request.params.hasOwnProperty('owner')) {
    query.equalTo('owner', request.params.owner);
  }

  const nftMongoPipeline = [
    {
      group: {
        objectId: {
          displayName: '$displayName',
          rarity: '$rarity',
          description: '$description',
          primarySalePrice: '$primarySalePrice',
        },
        total: {
          $sum: 1,
        },
        available: {
          $sum: {
            $cond: {
              if: {
                $eq: ['$status', 'available'],
              },
              then: 1,
              else: 0,
            },
          },
        },
        comingSoon: {
          $sum: {
            $cond: {
              if: {
                $eq: ['$status', 'comingSoon'],
              },
              then: 1,
              else: 0,
            },
          },
        },
        displayName: { $first: '$displayName' },
        rarity: { $first: '$rarity' },
        description: { $first: '$description' },
        primarySalePrice: { $first: '$primarySalePrice' },
        imageURL: { $first: '$imageURL' },
        songId: { $first: '$songId' },
        artistId: { $first: '$artistId' },
        mp3URL: { $first: '$mp3URL' },
      },
    },
    {
      project: {
        objectId: 0,
      },
    },
  ];

  const result = await query.aggregate(nftMongoPipeline);
  return result;
  
});

any suggestions? what am I missing here?

That cloud code looks ok, what is your serverUrl? If you tried to save it, it looks like it didn’t work.

You can copy and use the moralis-admin-cli command from your server’s Cloud Functions tab (from admin interface) - this is also where you can view existing cloud code.

server url: https://kklmsrfhqu3c.usemoralis.com:2053/server

i couldn’t find any interface under cloud functions tab what is the right way to “deploy” functions?

You use the moralis-admin-cli tool to save cloud code to your server - follow those instructions on that page you screenshotted.

When I say admin interface, I meant admin.moralis.io.

yeah… that’s exactly what I did. simply saving the cloud function code that Moralis is watching didn’t actually save (or deploy) functions to server… looks like it keeps listening the code whenever I make changes though

What is the exact command you’re using (you can hide the API key/secret)?

Make sure you are specifying a path (either full path or relative path from where you’re running moralis-admin-cli) to the folder that contains the cloud code file (.js), not the file itself.

moralis-admin-cli watch-cloud-folder --moralisApiKey xxxxxxx --moralisApiSecret xxxxxx --moralisSubdomain kklmsrfhqu3c.usemoralis.com --autoSave 1 --moralisCloudfolder ./moralis-cloud-functions/loadNFTs.js

i’m running this command in root folder of the project. and whenever I save the file while being watched, it logs the message Changes Uploaded Correctly

Screen Shot 2022-09-20 at 8.10.28 PM

./moralis-cloud-functions/loadNFTs.js

Remove loadNFTs.js, just use ./moralis-cloud-functions.

1 Like

thanks @alex I appreciate that
I think documentation needs to be updated so that “path” is clear to every devs