如何使用nodejs访问for循环外的变量值并在dialogflow中显示给用户
How to access variable values outside for loop and display to user in dialogflow using nodejs
对话流程:
用户:您好,我想订酒店。
Bot:sure.告诉我你的最小范围。
User:110$.
Bot:your最大射程?
User:180$.
Bot:I 查看 110$,145$,180$ 范围内的酒店。您想要哪一家 select?
在这里,我使用 firestore db 从数据库中检索范围从 110$ 到 180$ 的数据。
代码如下:
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)):-从 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'
}
]
如果我在 for 循环后使用 console.log(price),我只会显示最后的记录价格 - 180 美元...我尝试使用 push。但它没有用。
我是 Nodejs 的初学者。如何显示结果数组中的所有价格,如上面的对话流程所示?
有很多方法可以达到您想要的结果。
这里有一些方法可以做到这一点:
- 您可以将
console.log("price",price);
放在 for 循环中。
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);
}
上面的代码将在其进行的每个循环中打印价格。
它应该给你这个结果:
price 100
price 110
price 150
price 180
您可以测试代码 here.
- 可以使用数组推送。
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());
您可以测试代码here。
请查看此 Loops and iteration 指南以了解有关循环的更多信息。
对话流程:
用户:您好,我想订酒店。
Bot:sure.告诉我你的最小范围。
User:110$.
Bot:your最大射程?
User:180$.
Bot:I 查看 110$,145$,180$ 范围内的酒店。您想要哪一家 select?
在这里,我使用 firestore db 从数据库中检索范围从 110$ 到 180$ 的数据。
代码如下:
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)):-从 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'
}
]
如果我在 for 循环后使用 console.log(price),我只会显示最后的记录价格 - 180 美元...我尝试使用 push。但它没有用。
我是 Nodejs 的初学者。如何显示结果数组中的所有价格,如上面的对话流程所示?
有很多方法可以达到您想要的结果。
这里有一些方法可以做到这一点:
- 您可以将
console.log("price",price);
放在 for 循环中。
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);
}
上面的代码将在其进行的每个循环中打印价格。 它应该给你这个结果:
price 100
price 110
price 150
price 180
您可以测试代码 here.
- 可以使用数组推送。
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());
您可以测试代码here。
请查看此 Loops and iteration 指南以了解有关循环的更多信息。