我正在开发一个允许用户拍摄列表照片的项目,然后程序将允许用户使用cropperjs裁剪图像然后它会将裁剪后的图像传递到visionAPI,在那里它将提取文本frm图像。但是现在我面临这个2错误
异常:无法读取属性' forEach'未定义的
TypeError:无法读取属性' forEach'未定义的
编译器提供的一个提示就是这个
在CameraPage.getText(main.js:82525)
我不确定这里出了什么问题
以下是代码:
camera.ts
INSERT INTO #tempDates
SELECT DATEADD(DAY, nbr - 1, @fromDate)
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS Nbr
FROM sys.columns c
) nbrs
WHERE nbr - 1 <= DATEDIFF(DAY, @fromDate, @toDate)
camera.html
export class CameraPage {
public image:string;
width:number = 500;
height:number = 500;
quality:number = 90;
labels: Array<any> = [];
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public testService: TestService,
public cameraService: CameraService,
public toastCtrl: ToastController
) {}
addPhoto(){
this.cameraService.getImage(this.width,this.height,this.quality)
.subscribe( (data) => {
this.image = (data);
//console.log(btoa(this.image));
this.getVision(btoa(this.image));
//console.log(this.image);
},(error) => {
// Toast errot and return DEFAULT_PHOTO from Constants
this.toast(error);
}
);
}
toast(message: string) {
let toast = this.toastCtrl.create({
message: message,
duration: 2500,
showCloseButton: false
});
toast.present();
}
getVision(image64: string) {
this.testService.getVisionLabels(image64).subscribe((sub) => {
this.labels = sub.responses[0].textAnnotations;
this.getText();
});
}
getText() {
this.labels.forEach((label) => {
let translation = {search: label.description, result: ''};
console.log(label.description);
});
}
}
答案 0 :(得分:2)
错误的含义是this.labels
方法中getText()
未定义。
您应该在两个位置设置断点来分析问题:
this.labels = sub.responses[0].textAnnotations
行this.labels.forEach
行要检查的事项:
labels
属性。sub.responses[0]
以查看您的内容(textAnnotations
可能未定义)。