How to complete this mission in angular

Mission: SERVERLESS 101
Task: Invoke the API Service using generated client SDK

I struck here, I don’t know how to implement in Angular.

Hello @Farhan007

Could you provide details or ask specific questions about places where you are stuck?

Regards, Dima

Why I got these error?

Hello @Farhan007!

Could you please provide a code sample to reproduce the issue?

Regards,
Alexander

1 Like

  /*******************************************************************
  * CodelessShoppingCartService.js
  * Generated by Backendless Corp.
  ********************************************************************/
		
const Utils = {
  isObject : obj => obj === Object(obj),
  isString : obj => Object.prototype.toString.call(obj).slice(8, -1) === 'String',
  isNumber : obj => Object.prototype.toString.call(obj).slice(8, -1) === 'Number',
  isBoolean: obj => Object.prototype.toString.call(obj).slice(8, -1) === 'Boolean',
  isDate   : obj => Object.prototype.toString.call(obj).slice(8, -1) === 'Date'
}

Backendless.APIServices.CodelessShoppingCartService = {
  getInstructions() { 
    const args = null
      
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'getInstructions', args)
  },

  getItems(cartName) {   if (typeof cartName !== 'string') {
        throw new Error('Invalid value for argument "cartName". Must be an string object')
      }
      
    const args = cartName
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'getItems', args)
  },

  purchase(cartName) {   if (typeof cartName !== 'string') {
        throw new Error('Invalid value for argument "cartName". Must be an string object')
      }
      
    const args = cartName
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'purchase', args)
  },

  addItem(cartName,item) {   if (typeof cartName !== 'string') {
        throw new Error('Invalid value for argument "cartName". Must be an string object')
      }
        
    if (!Utils.isObject(item)) {
      throw new Error('Invalid value for argument "item". Must be object value')
    }
    
    const args = {
      cartName: cartName,
        item: item
    }
      
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'addItem', args)
  },

  deleteItem(cartName,productName) {   if (typeof cartName !== 'string') {
        throw new Error('Invalid value for argument "cartName". Must be an string object')
      }
        if (typeof productName !== 'string') {
        throw new Error('Invalid value for argument "productName". Must be an string object')
      }
      
    const args = {
      cartName: cartName,
        productName: productName
    }
      
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'deleteItem', args)
  },

  setQuantity(cartName,productName,quantity) {   if (typeof cartName !== 'string') {
        throw new Error('Invalid value for argument "cartName". Must be an string object')
      }
        if (typeof productName !== 'string') {
        throw new Error('Invalid value for argument "productName". Must be an string object')
      }
        if (typeof quantity !== 'number') {
        throw new Error('Invalid value for argument "quantity". Must be an number object')
      }
      
    const args = {
      cartName: cartName,
        productName: productName,
        quantity: quantity
    }
      
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'setQuantity', args)
  },

  Items(cartName,items) {   if (typeof cartName !== 'string') {
        throw new Error('Invalid value for argument "cartName". Must be an string object')
      }
        
    if (!Array.isArray(items)) {
      throw new Error('Invalid value for argument "items". Must be array value')
    }
    
    const args = {
      cartName: cartName,
        items: items
    }
      
    return Backendless.APIServices.invoke('CodelessShoppingCartService', 'Items', args)
  }
}

//mian.js
function getInstructions() {
  this.CdelessShopingCartService.getInstructions().then(function(result){
      log(result.join('\n'));
  })
}

function addItem() {
  this.CdelessShopingCartService.Item(cartName, 'mycart', item, {'name':'Milk', 'quantity': 1})
  .then(function(){
      log('Iten added');
  })
}

function getItem() {
  this.CdelessShopingCartService.getItem(cartName, 'mycart')
  .then(function(result){
      log(JSON.stringify(result));
  })
}

function purchase() {
  this.CdelessShopingCartService.purchase(cartName, 'mycart')
  .then(log('everything has been purchased'))
}

function log(message) {
  document.getElementById(elementId, 'output').innerText +=message + '\n';
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>A JavaScript project</title>
  <link rel="stylesheet" href="style.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <h1>A JavaScript project</h1>
  <a href="javascript:getInstructions()">Get Instructions</a><br>
  <a href="javascript:addItem()">Add Item</a><br>
  <a href="javascript:getItem()">Get Items</a><br>
  <a href="javascript:purchase()">Purchase</a><br>

  <div id="app"></div>
  <script type="text/javascript" src="https://api.backendless.com/sdk/js/latest/backendless.min.js"></script>
  <script type="text/javascript" src="../js/services/CodelessShoppingCartService.js" ></script>
  <script src="main.js"></script>
</body>
</html>

You forgot to initialize the application:

Backendless.initApp( "https://your-subdomain-name.backendless.app" );

Regards,
Alexander

Hi @Alexander_Pavelko,

I’m experiencing the same issue.

Where does
Backendless.initApp( “https://your-subdomain-name.backendless.app” );
go? What file and where?

And what is my “your-subdomain-name”

Thank you.

Charles

Hi @charles_lim ,

The initApp should be placed in the initialization logic of your app. I recommend you to start with a project template that can be downloaded from Backendless Console:

Step 1:

Step 2:

Step 3:

This will download a zip file that contains Angular project structure.

The initApp call will be already included in the app.components.ts file.

As for your-subdomain-name, you can read in the docs at:
https://backendless.com/docs/js/setup.html

Regards,
Mark

thank you very much, @mark-piller!

it works!!

Charles