Closed TLKG closed 5 years ago
@TLKG is it possible you're not following that sample precisely? That method gets put on the client via mixin, so it needs to be imported exactly as the sample suggests.
This is my ocr.js:
const IncomingForm = require('formidable').IncomingForm; //using formidable to handle file upload
module.exports = function upload(req, res) {
var form = new IncomingForm(...);
form.on('fil', (field, file) => {
const vision = require('@google-cloud/vision');
const ocr = new vision.ImageAnnotatorClient();
const [result] = await ocr.documentTextDetection(file.path); // <----- generate error
ocr.documentTextDetection(file.path) // <------ works
.then()
@TLKG so to clarify you are saying is if you don't use await
that is when an error occurs?
@callmehiphop When I used the exact official code inside function upload()
, documentTextDetection()
was highlighted saying
documentTextDetection is not a function
Same if take away await
.
Keep await
and added async
in front of function upload
resulted
await is only valid in async function
So, what I posted above is how I made it work.
@TLKG unfortunately I'm unable to reproduce this, if there is anything else you can think of that might help me reproduce, please let us know!
@TLKG could you please share a full stack trace of the error that is generated when using await?
Surely will do. Thanks
Sent from my iPad
On Sep 12, 2019, at 5:44 PM, Benjamin E. Coe notifications@github.com wrote:
@TLKG could you please share a full stack trace of the error that is generated when using await?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
Running official code for local image, only added where the .jpg is:
// Imports the Google Cloud client library
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 = 'test1.jpg';
// Read a local image as a text document
const [result] = await client.documentTextDetection(fileName);
const fullTextAnnotation = result.fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
fullTextAnnotation.pages.forEach(page => {
page.blocks.forEach(block => {
console.log(`Block confidence: ${block.confidence}`);
block.paragraphs.forEach(paragraph => {
console.log(`Paragraph confidence: ${paragraph.confidence}`);
paragraph.words.forEach(word => {
const wordText = word.symbols.map(s => s.text).join('');
console.log(`Word text: ${wordText}`);
console.log(`Word confidence: ${word.confidence}`);
word.symbols.forEach(symbol => {
console.log(`Symbol text: ${symbol.text}`);
console.log(`Symbol confidence: ${symbol.confidence}`);
});
});
});
});
});
Debug console output:
C:\Program Files\nodejs\node.exe --inspect-brk=19329 index.js Debugger listening on ws://127.0.0.1:19329/f68e3fc3-9225-4bb2-9425-55835e796886 For help, see: https://nodejs.org/en/docs/inspector Debugger attached. c:\test1\index.js:14 const [result] = await client.documentTextDetection(fileName); ^^^^^ SyntaxError: await is only valid in async function at Module._compile (internal/modules/cjs/loader.js:723:23) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3) Waiting for the debugger to disconnect... SyntaxError: await is only valid in async function at Module._compile (internal/modules/cjs/loader.js:723:23) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
when plug into my own code
C:\Program Files\nodejs\node.exe --inspect-brk=20527 server.js Debugger listening on ws://127.0.0.1:20527/9f44dd6a-cb02-4df5-b3e5-ccc4e02a3f20 For help, see: https://nodejs.org/en/docs/inspector Debugger attached. API listening at 3000 ! TypeError: client.documentTextDetection is not a function or its return value is not iterable at IncomingForm.form.on (c:\Api new\ocr.js:22:26) at IncomingForm.emit (events.js:198:13) at c:\Api new\node_modules\formidable\lib\incoming_form.js:237:12 at WriteStream.
(c:\Api new\node_modules\formidable\lib\file.js:79:5) at Object.onceWrapper (events.js:286:20) at WriteStream.emit (events.js:198:13) at finishMaybe (_stream_writable.js:646:14) at stream._final (_stream_writable.js:624:5) at WriteStream._final (internal/fs/streams.js:268:3) at callFinal (_stream_writable.js:617:10)
nodejs 10.16.3, 64-bit, here is my own depenencies
dependencies": {
"@google-cloud/vision": "^1.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"formidable": "^1.2.1",
"fs-extra": "^8.0.1"
}
@TLKG looking at your first example, it looks like maybe you're trying to use a top level await? I believe that is what the error is trying to indicate as well. Could you try wrapping that portion into an async function?
@TLKG
async function main () {
// Imports the Google Cloud client library
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 = 'test1.jpg';
// Read a local image as a text document
const [result] = await client.documentTextDetection(fileName);
const fullTextAnnotation = result.fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
fullTextAnnotation.pages.forEach(page => {
page.blocks.forEach(block => {
console.log(`Block confidence: ${block.confidence}`);
block.paragraphs.forEach(paragraph => {
console.log(`Paragraph confidence: ${paragraph.confidence}`);
paragraph.words.forEach(word => {
const wordText = word.symbols.map(s => s.text).join('');
console.log(`Word text: ${wordText}`);
console.log(`Word confidence: ${word.confidence}`);
word.symbols.forEach(symbol => {
console.log(`Symbol text: ${symbol.text}`);
console.log(`Symbol confidence: ${symbol.confidence}`);
});
});
});
});
}
main();
:point_up_2: something like that.
We should make sure the docs show the async wrapper as well.
Can you give an example please?
Sent from my iPad
On Sep 17, 2019, at 11:24 AM, Stephen notifications@github.com wrote:
We should make sure the docs show the async wrapper as well.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
Will let you know. Thanks
Sent from my iPad
On Sep 17, 2019, at 10:55 AM, Benjamin E. Coe notifications@github.com wrote:
@TLKG
async function main () {
// Imports the Google Cloud client library 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 = 'test1.jpg';
// Read a local image as a text document const [result] = await client.documentTextDetection(fileName); const fullTextAnnotation = result.fullTextAnnotation; console.log(
Full text: ${fullTextAnnotation.text}
); fullTextAnnotation.pages.forEach(page => { page.blocks.forEach(block => { console.log(Block confidence: ${block.confidence}
); block.paragraphs.forEach(paragraph => { console.log(Paragraph confidence: ${paragraph.confidence}
); paragraph.words.forEach(word => { const wordText = word.symbols.map(s => s.text).join(''); console.log(Word text: ${wordText}
); console.log(Word confidence: ${word.confidence}
); word.symbols.forEach(symbol => { console.log(Symbol text: ${symbol.text}
); console.log(Symbol confidence: ${symbol.confidence}
); }); }); }); });}
main(); 👆 something like that.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
Still
SyntaxError: await is only valid in async function
This is my api: [server.js]
const express = require('express')
const server = express()
const cors = require('cors');
const ocr = require('./ocr');
var corsOptions =
{ origin: '*', optionsSuccessStatus: 200, }
server.use(cors(corsOptions));
server.post('/uploads', ocr);
server.get('/', ()=>{
console.log('something GET.');
})
server.listen(3000, () => {
console.log('API listening at 3000 !')
})
[ocr.js]
const IncomingForm = require('formidable').IncomingForm;
async function upload(req, res) {
var form = new IncomingForm({ uploadDir: 'C:\\AllUploads' }); // maps the './uploads' to the physical folder for files to be uploaded to
form.on('file', (field, file) => {
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
const [result] = await client.documentTextDetection(file.path);
const fullTextAnnotation = result.fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
fullTextAnnotation.pages.forEach(page => {
page.blocks.forEach(block => {
console.log(`Block confidence: ${block.confidence}`);
block.paragraphs.forEach(paragraph => {
console.log(`Paragraph confidence: ${paragraph.confidence}`);
paragraph.words.forEach(word => {
const wordText = word.symbols.map(s => s.text).join('');
console.log(`Word text: ${wordText}`);
console.log(`Word confidence: ${word.confidence}`);
word.symbols.forEach(symbol => {
console.log(`Symbol text: ${symbol.text}`);
console.log(`Symbol confidence: ${symbol.confidence}`);
});
});
});
});
});
form.on('end', () => {})
form.parse(req)
});
}
module.exports.upload= upload;
Full stack output: const [result] = await client.documentTextDetection(file.path); ^^^^^
SyntaxError: await is only valid in async function
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.
@TLKG looks like the issue is that you need to make your file event handler an async function.
form.on('file', async (field, file) => {
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
const [result] = await client.documentTextDetection(file.path);
});
@callmehiphop
Good point, I was just thinking of function
.
Updated, now:
Error: Route.post() requires a callback function but got a [object Object]
see updated code below:
ocr.js
const IncomingForm = require('formidable').IncomingForm;
async function upload(req, res) {
var form = new IncomingForm({ uploadDir: 'C:\\AllUploads' }); // maps the './uploads' to the physical folder for files to be uploaded to
form.on('file', async (field, file) => {
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
const [result] = await client.documentTextDetection(file.path);
const fullTextAnnotation = result.fullTextAnnotation;
console.log(`Full text: ${fullTextAnnotation.text}`);
fullTextAnnotation.pages.forEach(page => {
page.blocks.forEach(block => {
console.log(`Block confidence: ${block.confidence}`);
block.paragraphs.forEach(paragraph => {
console.log(`Paragraph confidence: ${paragraph.confidence}`);
paragraph.words.forEach(word => {
const wordText = word.symbols.map(s => s.text).join('');
console.log(`Word text: ${wordText}`);
console.log(`Word confidence: ${word.confidence}`);
word.symbols.forEach(symbol => {
console.log(`Symbol text: ${symbol.text}`);
console.log(`Symbol confidence: ${symbol.confidence}`);
});
});
});
});
});
});
}
module.exports.upload= upload;
complete stack output:
C:\Program Files\nodejs\node.exe --inspect-brk=28915 server.js
Debugger listening on ws://127.0.0.1:28915/e25c7fa5-8f39-45d8-be4c-dbb266f7fbb7
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
c:\Api new\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^
Error: Route.post() requires a callback function but got a [object Object]
at Route.(anonymous function) [as post] (c:\Api new\node_modules\express\lib\router\route.js:202:15)
at Function.app.(anonymous function) [as post] (c:\Api new\node_modules\express\lib\application.js:482:19)
at Object.<anonymous> (c:\Api new\server.js:14:8)
at Module._compile (internal/modules/cjs/loader.js:775:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
Waiting for the debugger to disconnect...
Error: Route.post() requires a callback function but got a [object Object]
at Route.(anonymous function) [as post] (c:\Api new\node_modules\express\lib\router\route.js:202:15)
at Function.app.(anonymous function) [as post] (c:\Api new\node_modules\express\lib\application.js:482:19)
at Object.<anonymous> (c:\Api new\server.js:14:8)
at Module._compile (internal/modules/cjs/loader.js:775:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
server.js
const express = require('express')
const server = express()
const cors = require('cors');
const ocr = require('./ocr');
var corsOptions =
{
origin: '*',
optionsSuccessStatus: 200,
}
server.use(cors(corsOptions));
server.post('/uploads', ocr);
server.get('/', ()=>{
console.log('something GET.');
})
server.listen(3000, () => {
console.log('API listening at 3000 !')
})
Object.server.post('/uploads', ocr);
Route.post() requires a callback function but got a [object Object]
is resolved by changing module.exports.upload= upload;
to module.exports = upload;
.
But now upload() is no longer triggered.
@TLKG I don't suppose you could create an example GitHub repo, that demonstrates the issues you're having with your express application? It's easier to see what's happening if we can run the application.
@bcoe You are right. Original issue is resolved! Thank you all.
@TLKG awesome \o/ let me know if you bump in to any more problems.
Environment details
@google-cloud/vision
version: 1.2.0Steps to reproduce
const [result] = await client.documentTextDetection(fileName);
I'm able to call
.textDetection()
Thanks!