Problem with async / await - Syntaxerror: await is only Valid in async Function [closed]

Asked

Viewed 990 times

-1

Hello

I’m trying to set up a function but without success. I use a "client.classifyImageUrl" function that will process a cognitive service, when finished, I need to take the result value (more specifically: result.Predictions[0].tagname) and return to the bot with step.context.sendActivity but I’m not sure how to do it. I left the code as follows but is generating an error of: Syntaxerror: await is only Valid in async Function when trying to start the service (npm start). Could you help me?

async identificarSabor(step) {
        endDialog = false;

        const credentials = new ApiKeyCredentials({ inHeader: { "Prediction-key": process.env.CustonVisionKey } });
        const client = new PredictionAPIClient(credentials, process.env.CustonVisionEndpoint);

        //console.log(step.context.activity['attachments'][0].contentUrl);

        console.log(result);

        client
            .classifyImageUrl(process.env.CustonVisionProjectId, process.env.CustonVisionIteration,
                { url: step.context.activity['attachments'][0].contentUrl })
            .then(result => {
                if ('predictions' in result) {
                    await step.context.sendActivity(result.predictions[0].tagName);
                }
            })
            .catch(err => {
                //step.context.sendActivity("Não consegui identificar a pizza nessa imagem. Tente enviar outra imagem!");
                console.error(err);
            });

        return await step.continueDialog();
    }

1 answer

0

Very simple. Tu ta mistura async/await com then/Cath.

return await step.continueDialog();

take this return right there and access his information via .then even.

step.continueDialog()
  .then( value => return value )
  .catch (err => throw new Error('deu erro')
  • Actually the problem I am is in this line: await step.context.sendActivity(result.Predictions[0].tagname); I’m not being able to return "tagname" using the sendActivity function. If I take it out of the classifyImageUrl function I don’t have the result, if I put it in, the error in the function use, either with await or without. I don’t understand how to do this.

  • Well, it’s the same. Tu está chamar um await dentro de um bloco .then... Grab it step.context.sendActivity(result.Predictions[0]. tagname) . then( value => value) . ( catch value => value

  • Got it, I’ll try, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.