Javascript 如何修复SyntaxError:await仅在Google cloud vision的异步函数中有效
我想使用谷歌云视觉API。我复制了代码,但出现以下错误:SyntaxError:await仅在异步函数中有效Javascript 如何修复SyntaxError:await仅在Google cloud vision的异步函数中有效,javascript,node.js,async-await,Javascript,Node.js,Async Await,我想使用谷歌云视觉API。我复制了代码,但出现以下错误:SyntaxError:await仅在异步函数中有效 const vision = require('@google-cloud/vision'); // Creates a client const client = new vision.ImageAnnotatorClient(); /** * TODO(developer): Uncomment the following line before running the sam
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
const fileName = '/Tickets/leclerc.jpg';
// Performs text detection on the local file
const [result] = await client.textDetection(fileName);
const detections = result.textAnnotations;
console.log('Text:');
detections.forEach(text => console.log(text));
有没有办法解决这个问题
非常感谢需要将等待方法封装到异步函数中,这很容易修复
const detectLocalFile = async function() {
const [result] = await client.textDetection(fileName);
{...}
}
将代码放入
async
函数中?我的意思是,错误很明显。非常感谢,它很有效。