使用 dialogflow 对列中的值求和
Sum up values in a column using dialogflow
我是 dialogflow 的新手,想计算列(年龄)中所有值的总和。我的数据结构如下,我基本上尝试检索这些值。
我的数据结构
请帮助总结年龄列中的所有值。
function detectage(agent){
return admin.database().ref('data').child(agent.parameters.name).once('value').then((snapshot) => {
const value = snapshot.val();
const age = value.Age;
if(value !== null){
agent.add(`The age value from database is ${age}`);
}
});
}
Dialogflow 本身不提供任何可以为您求和的值。虽然您可以在履行功能中执行此操作,但 Dialogflow 不会自动执行此操作。
您的函数看起来像是从 Firebase 数据库中提取单个记录,而不是遍历 data
节点下的所有子节点并对 Age
[=每个子节点的 19=]。
执行此操作的代码可能看起来更像
function detectage(agent){
return admin.database().ref('data').once('value')
.then((snapshot) => {
let age = 0;
snapshot.forEach( childSnapshot -> {
const value = childSnapshot.val();
age += value.Age;
});
if(age !== null){
agent.add(`The total age from database is ${age}`);
}
});
}
我是 dialogflow 的新手,想计算列(年龄)中所有值的总和。我的数据结构如下,我基本上尝试检索这些值。
我的数据结构
请帮助总结年龄列中的所有值。
function detectage(agent){
return admin.database().ref('data').child(agent.parameters.name).once('value').then((snapshot) => {
const value = snapshot.val();
const age = value.Age;
if(value !== null){
agent.add(`The age value from database is ${age}`);
}
});
}
Dialogflow 本身不提供任何可以为您求和的值。虽然您可以在履行功能中执行此操作,但 Dialogflow 不会自动执行此操作。
您的函数看起来像是从 Firebase 数据库中提取单个记录,而不是遍历 data
节点下的所有子节点并对 Age
[=每个子节点的 19=]。
执行此操作的代码可能看起来更像
function detectage(agent){
return admin.database().ref('data').once('value')
.then((snapshot) => {
let age = 0;
snapshot.forEach( childSnapshot -> {
const value = childSnapshot.val();
age += value.Age;
});
if(age !== null){
agent.add(`The total age from database is ${age}`);
}
});
}