1

I have getting some issue in map program in node.js. I am trying to use MAP into node.js program and I am not getting value.I have write some code to use map however I think it not working as its not go into the loop to fetch the key-value.

Please find below program

  async CreateProduceMVPRateAsset(data, callback) {

     var ProducePRICE = {};

             var MVPPRICE =[];
             var MVPPRICE_BS ={};
             var MVPPRICE_LB ={};
             //var ASKINGPRICE= {}// put all the things which need to go to blockchain
            const walletPath = path.join(process.cwd(), 'wallet');
            const wallet = new FileSystemWallet(walletPath);
            console.log(`Wallet path: ${walletPath}`);
            console.log('Data', data);
            console.log('Username', data.username);

 var PRODUCENAME = data.PRODUCE
            console.log('PRODUCENAME', PRODUCENAME);

            var COUNTRY = data.COUNTRY;
            console.log('COUNTRY', COUNTRY);
            var STATE = data.STATE;
            console.log('STATE', STATE);


          var MVPRATES =  data.MVPRATES;  

             console.log('MVPRATERATE', MVPRATES);

             const MVPRATE = new Map(MVPRATES);

// program could not go inside the for loop
             for (const [k, v] of MVPRATE.entries()) {
                 console.log("Inside map", k, v)
                 MVPPRICE = v.RATE  // should go in  MVPPRICE 

                  var Value =  MVPPRICE[0].value // want to get first element value from MVPPRICE array 
                  console.log('Value', Value);

                 var value_lb = Value/40;
                 console.log('value_lb', value_lb);

                 value_lb = Number((value_lb).toFixed(4));

                    console.log('If the value of BS provided controller come here');

                    MVPPRICE_LB.Value = value_lb
                    MVPPRICE_LB.QuantityUnit = 'LB'
                    MVPPRICE_LB.uidisplay = false
                    MVPPRICE_LB.CurrencyUnit = 'USD'

                    MVPPRICE.push(MVPPRICE_LB);
                    ProducePRICE.MVPPRICE = MVPPRICE

                        console.log('MVPPRICE',MVPPRICE);
                   console.log('ProducePRICE',ProducePRICE);

                    // mvpprice.totalPrice = totalPrice;
                    console.log('for pricing',totalPrice);

                    // ProducePRICE.VARIETY = VARIETY;
                    ProducePRICE.PRODUCENAME = PRODUCENAME;
                    ProducePRICE.STATE = STATE;
                    ProducePRICE.COUNTRY = COUNTRY;

              }

JSON structure which I am sending using postman

{
"username": "admin2",
  "PRODUCE": "Apple",
  "STATE": "MI",
  "COUNTRY": "US",
  "MVPRATES": {
    "fuji": {
      "VARIETY": "fuji",
      "RATE": [
        {
          "UNIT": "Bussel",
          "CURRENCY": "USD",
          "VALUE": 10.25,
          "UIDISPLAY": true
        }
      ]
    },
    "gala": {
      "VARIETY": "gala",
      "RATE": [
        {
          "UNIT": "Bussel",
          "CURRENCY": "USD",
          "VALUE": 10.25,
          "UIDISPLAY": true
        }
      ]
    }
  }
}

Output which I am getting: enter image description here

8
  • odd you don't get an error TypeError: object is not iterable considering data.MVPRATES is not iterable Commented Oct 7, 2019 at 9:09
  • @ Bravo I don't get this error as its not go inside the loop I think Commented Oct 7, 2019 at 9:11
  • Oh, the error should come from const MVPRATE = new Map(MVPRATES); ... before the loop - are you calling CreateProduceMVPRateAsset in a try/catch perhaps, and ignoring the error? and why is CreateProduceMVPRateAsset declared async since a) it doesn't do any await and b) there is no reason for it to be asynchronouos Commented Oct 7, 2019 at 9:12
  • Ok @ I made it like const MVPRATE = new Map(); It run but still its not go inside the for loop and skip the loop . Commented Oct 7, 2019 at 9:15
  • of course, because the Map is empty, therefore nothing to iterate - what is the purpose of using Map? new toy? or is there a particular reason you want to do that? Commented Oct 7, 2019 at 9:16

1 Answer 1

1

Seems like you may want to do the following, this creates a Map with two entries, fuji and gala

const MVPRATES = {
  "fuji": {
    "VARIETY": "fuji",
    "RATE": [{
      "UNIT": "Bussel",
      "CURRENCY": "USD",
      "VALUE": 10.25,
      "UIDISPLAY": true
    }]
  },
  "gala": {
    "VARIETY": "gala",
    "RATE": [{
      "UNIT": "Bussel",
      "CURRENCY": "USD",
      "VALUE": 10.25,
      "UIDISPLAY": true
    }]
  }
}
const MVPRATE = new Map(Object.entries(MVPRATES));
for (const [k, v] of MVPRATE.entries()) {
  console.log("Inside map", k, v)
}

Though, the need for Map is still unclear, as you can achieve the above with

const MVPRATES = {
  "fuji": {
    "VARIETY": "fuji",
    "RATE": [{
      "UNIT": "Bussel",
      "CURRENCY": "USD",
      "VALUE": 10.25,
      "UIDISPLAY": true
    }]
  },
  "gala": {
    "VARIETY": "gala",
    "RATE": [{
      "UNIT": "Bussel",
      "CURRENCY": "USD",
      "VALUE": 10.25,
      "UIDISPLAY": true
    }]
  }
}
for (const [k, v] of Object.entries(MVPRATES)) {
  console.log("Inside map", k, v)
}

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks soo much @ Bravo I have to check with my need, however your solution helpful
Hi @ Bravo I have small issue here , I have used var Value = MVPPRICE[0].value, which is give undefined could you please suggest me why? if possible
oh ... becauase value is not VALUE as they are two different things (javascript is case sensitive)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.