mysql - 如何在异步函数中发出post请求?

标签 mysql node.js botframework

在“摘要”中的 waterfall 对话框的末尾(即最后一个 if 语句),我想自动发出 post 请求,而不在 Postman 中进行 API 调用,是 eventListener 吗?如何包含它?

async summaryStep(step) {
            if (step.result) {
                // Get the current profile object from user state.
                const userProfile = await this.userProfile.get(step.context, new UserProfile());
                userProfile.name = step.values.name;
                //the same for other step values(email, doctor, date)
                let msg = `you want a date with dr. ${userProfile.doctor} , and your name is ${userProfile.name}.`;
                if (userProfile.date !== -1) {
                    msg += `you have an appointment the:  ${userProfile.date}.`;
                }
                await step.context.sendActivity(msg);
                let msg1 = `"${userProfile.date}"`;
                if (msg1) {
                    let z = JSON.stringify(userProfile.name);
                    //and also the other rows to go in the database(email, doctor, date)
                    var name = JSON.parse(z);
                    //and also the other rows to go in the database(email, doctor, date)
                    //this actually works but only if i use postman 
                    var urlencoded = bodyparser.urlencoded({ extended: false });
                    app.post('/id', urlencoded, (req, res) => {
                        app.use(express.json());
                        app.use(express.urlencoded({ extended: true }));
                        mysqlConnection.query("INSERT INTO users(name, email, doctor, date) VALUES('" + userProfile.name + "','" + userProfile.password + "','" + userProfile.doctor + "','" + userProfile.date + "')", function (err, result, rows) {
                            if (err) throw err;
                            console.log("Yeah! record inserted");
                            console.log(name);
                            res.send(result);
                        });
                    });
                    const port = process.env.PORT || 8080;
                    app.listen(port, () => console.log(`Listening on port ${port}..`));
                }

            } else {
                await step.context.sendActivity('Thanks. Your profile will not be kept. Push enter to return Menu');
            }
            return await step.prompt(CONFIRM_PROMPT3, `is that true? ${step.result}`, ['yes', 'no']);
// this if statement should "fire" the post request...
            if (step.result == 'yes') {
                return await step.context.sendActivity(`we will contact you soon ${userProfile.password}.`);
            }
            return await step.endDialog();
        }

enter image description here

最佳答案

根据我的理解,您想知道如何从 Azure 机器人异步函数调用 POST API。请在您的async SummaryStep函数中尝试以下代码,根据您的要求发送发布请求。

var rp = require('request-promise');

var options = {
  method: 'POST',
  uri: 'http://localhost:8080/id',
  body: {
    fieldCount:0,
    affectedRows:1,
    //your other body content here...
  },
  json: true,
  headers: {
    'content-type': 'application/json'  //you can append other headers here 
  }
};

await rp(options)
    .then(function (body) {
        console.log(body)
    })
    .catch(function (err) {
      console.log(err)
    });
}

希望有帮助。 A 如果有任何进一步的疑虑或误解,请随时告诉我。

关于mysql - 如何在异步函数中发出post请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285990/

相关文章:

mysql - INSERT ... ON DUPLICATE KEY 语句的任何错误

php - Mysql关系一对多获取数据多where子句

javascript - Node-slack web api : chat. delete 返回所有 channel 的 channel_not_found 尽管 channels.list 返回所有 channel

azure - MS bot服务消息端点可以检测用户的编辑行为吗?

azure - Bot框架模拟器: Unknown Host

c# - 如何使用 C# 在瀑布对话框中调用 QnA Maker?

mysql - 我可以在现有数据库上使用 mysqlslap 吗?

mysql - 将 GMT 时间转换为 EST

javascript - 使用 PostgreSQL 选择查询的 Knex 在多个并行请求上性能极度下降

node.js - 在客户端计算机上获取 sqlite3 而无需他们编译和安装库