Search code examples
node.jsdialogflow-es

How to access variable values outside for loop and display to user in dialogflow using nodejs


Conversation Flow:

User:hi,I want to book a hotel.
Bot:sure.tell me your minimum range.
User:110$.
Bot:your maximum range?
User:180$.
Bot:I see the hotels of range 110$,145$,180$.Which one you would like to select?

Here,I use firestore db to retreive the data from database that ranges from 110$ to 180$.

Code below:

const snapshot = await db.collection('hoteldetails').where('Price', '<=', maxnumber) .where('Price','>=',minnumber).get()
      var result=snapshot.docs.map(doc => doc.data());      
      if(result.length!=0){
      for(let i =0;i<result.length;i++)
        {                               
            var hotelname=result[i].HotelName;
            var city=result[i].City;
            var price=result[i].Price;                                                    
        }
        console.log("price",price);

Result:(console.log("result",result)):-retrieves the records from Firestore.

result [
  {
    Price: 100,
    Availability: 'Yes',
    City: 'Mumbai',
    HotelName: 'GrandHyatt'
  },
  {
    HotelName: 'Leelaplace',
    Price: 110,
    City: 'Banglore',
    Availability: 'Yes'
  },
  {
    HotelName: 'OberaiHotel',
    City: 'Mumbai',
    Availability: 'Yes',
    Price: 150
  },
  {
    HotelName: 'Taj Hotel',
    Availability: 'yes',
    Price: 180,
    City: 'Mumbai'
  }
]

If I use console.log(price) after for loop,I get only last record price displayed-180$...I tried using push .But It did not work.

I am beginner to Nodejs. How can I display all the prices from result array as shown in above conversation flow?


Solution

  • There are many ways you could achieve the result that you want.

    Here are some ways on how you can do it:

    1. You can put the console.log("price",price); within the for loop.
    const snapshot = await db.collection('hoteldetails').where('Price', '<=', maxnumber) .where('Price','>=',minnumber).get()
          var result=snapshot.docs.map(doc => doc.data());      
          if(result.length!=0){
          for(let i =0;i<result.length;i++)
            {                               
                var hotelname=result[i].HotelName;
                var city=result[i].City;
                var price=result[i].Price;  
     
                console.log("price",price);                                                 
            }
    

    The code above will print the price on each loop that its making. It should give you this result:

    price 100
    price 110
    price 150
    price 180
    

    You could test the code here.

    1. You can use array push.
    const snapshot = await db.collection('hoteldetails').where('Price', '<=', maxnumber) .where('Price','>=',minnumber).get()
          var result=snapshot.docs.map(doc => doc.data());      
          if(result.length!=0){
          var prices = [];
          for(let i =0;i<result.length;i++)
            {                               
                var hotelname=result[i].HotelName;
                var city=result[i].City;
                var price=result[i].Price;  
    
                prices.push(price);
            }
    
            //".toString()" converts array to string. You can remove ".toString()" if you want it as an array.
            console.log(prices.toString());
    

    You can test the code here.

    Please check this Loops and iteration guide for more information about looping.