【问题标题】:Function call is Repeating in node js函数调用在节点 js 中重复
【发布时间】:2020-11-27 05:17:29
【问题描述】:

我是节点 js 的新手。我已经使用了数据来自的 python 调用。由于该过程有点复杂,因此执行和节点 js 接收响应需要时间。我在这里提到的代码试图等待函数完成,但函数从头开始重复调用并一直重复直到 python 函数完成并获得响应。 所以功能完成后

console.log("Image Conversation::")
await bot.say(dumy_msg);
await bot.beginDialog('classfication_done_'); 

这些语句重复了两次。

如何解决问题?

convo1.ask('Post Your message', async(answer, convo1, bot,message) => {
  if(message.type=='file_share') {

  console.log("Image Conversation::")
  const botToken = bot.api.token;

  const destination_path = '/_classification/upload/' + message.files[0].name;
  console.log("URl :::",message.files[0].url_private)
  console.log('Testing file share' + message.files[0].name);

  const url = message.files[0].url_private;
  const opts = {
    method: 'GET',
    url: url,
    headers: {
      Authorization: 'Bearer ' + botToken,
    }
  };
  request(opts, function (err, res, body) {
   
    console.log('FILE RETRIEVE STATUS', res.statusCode);

  }).pipe(fs.createWriteStream(destination_path)); 

  const img = '/_classification/upload/' + message.files[0].name;


  spawn = require("child_process").spawn;

  const getResponse = () => {
    return new Promise((resolve, reject) => {
    const process = spawn('python',["/_classification/tensorflow-image-detection-master/classify.py",img] );
    process.stdout.on('data', data => {
        resolve(data);
    });
    });
}
const response = await getResponse();
var dumy_msg = `${response}`;
await bot.say(dumy_msg);
     
     await bot.beginDialog('classfication_done_');
  }
  
  else{
    await bot.say(message,"Please provide jpeg file format, other files are not allowed");
    await bot.beginDialog('classfication_done_');
  }
});

【问题讨论】:

    标签: javascript python node.js asynchronous async-await


    【解决方案1】:

    我认为回调函数(async () => {}) 本身已经被调用了两次。

    我不确切知道convo1.ask 的作用是什么,但请确保它不会在任何情况下调用两次回调。

    【讨论】:

    • 我尝试删除 python 函数和其他代码,除了 await 'bot.say("PlainText");'在 convo1.ask 里面。但现在它只被正确调用了一次。
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2021-08-21
    • 1970-01-01
    • 2017-08-27
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多