函数调用在节点js中重复

2024-06-16 17:20:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我是NodeJS的新手。我使用了python调用,数据来自哪里。因为这个过程有点复杂,所以执行和接收响应都需要时间。我在这里提到的代码试图等待函数完成,但是函数从一开始就重复调用,直到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_');
  }
});

Tags: 函数logurlmessagebotmsgfilesawait