代码之家  ›  专栏  ›  技术社区  ›  MyNameIsGuzse

尝试调用“model.predict”时出现Clarifai对象错误

  •  0
  • MyNameIsGuzse  · 技术社区  · 5 年前

    private app;
    
    obj: RootObject ;
    
    constructor(private _http: HttpClient) {
        this.app = new Clarifai.App({
            ApiKey: "CENSOR BAR"
        });
    };
    
    public getColorValues(imageUrl: string): RootObject {
        this.app.models.predict('eeed0b6733a644cea07cf4c60f87ebb7', imageUrl).then(
            function (response) {
                this.obj = response;
            },
            function (error) {
                this.obj = "There was an error";
            }
        );
        let i: number;
        while (this.obj == null) {
            i += 1;
        }
        console.log("Waited " + i + " cycles for response.")
        console.log("Object: " + this.obj);
        return this.obj;
    }
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   Poul Kruijt    5 年前

    async ,但您正在将其作为同步调用处理。他回来了 this.obj

    除此之外,它根本不会被设置,因为您正在使用 function 关键字更改 this 对本地函数的引用

    getColorValues Promise<RootObject> :

    getColorValues(imageUrl: string): Promise<RootObject> {
      return this.app.models.predict('eeed0b6733a644cea07cf4c60f87ebb7', imageUrl);
    }
    

    就这样,这就是你所需要的。当你打电话给 获取颜色值

    getColorValues(imageUrl).then((resp) => {
      this.obj = resp;
      // this.obj contains what you want
    });
    
    推荐文章