【问题标题】:Angular 2/Mongoose: Called server function not invokedAngular 2/Mongoose:未调用调用的服务器函数
【发布时间】:2018-02-03 23:23:05
【问题描述】:

我的服务中有两个几乎相同的函数:getProducts() 和 getProductsByType(),它们调用服务器。 getProductsByType() 调用服务器函数并返回数据。 getProducts() 函数调用其对应的服务器函数,但从不调用服务器函数。

服务功能:

@Injectable()
export class DataService {
  private products: Product[];  

  private productsUrl: string = "/api/products/all";
  private productUrl: string = "/api/products/";

  constructor(private http: Http) { 
    this.getProducts();
  }

  public getProducts() {
    console.log("DataService.getProducts called");

    this.http.get(this.productsUrl)
    .map((response: Response) => {
      this.products = response.json();
    })
    .catch(this.handleError)    
  }

  public getProductsByType(type: string): Observable<Product[]> {
    console.log("DataService.getProductsByType called");

    return this.http.get(this.productUrl + type)
    .map((response: Response) => {
      return response.json() as Product[] || null;
    })
    .catch(this.handleError)    
  }
}

服务器功能:

router.get("/products/:type", function (req, res) {
console.log("Get product by type called")

Product.find({ "producttype": req.params.type })
    .exec(function (err, products) {
        if (err) {
            console.log("Error retrieving products: " + req.params.type);
            res.json(err);
        } else {
            // console.log("products = " + JSON.stringify(products));
            res.json(products);
        }
    });
})

router.get("/products/all", function (req, res) {
    console.log("Get product called")

    Product.find({})
        .exec(function (err, products) {
            if (err) {
                console.log("Error retrieving products");
                res.json(err);
            } else {
                console.log("products = " + JSON.stringify(products));
                res.json(products);
            }
        });
})

【问题讨论】:

  • 嗯,所以您没有看到 console.log("Get product called") 被记录?或者你看不到 console.log("products = " + JSON.stringify(products));正在登录?
  • 两者都没有记录。
  • 对不起,自从我使用快递以来已经有一分钟了。也许 express 正在将“/api/products/all”与您的第一条路线匹配,并将“all”解释为参数。也许尝试重新排序路线或在“/products/:type”路线中调用 next()。我得走了,对不起,我帮不上什么忙。祝你好运!
  • 去掉两个地方的/all来测试
  • 我已经这样做了。结果相同。

标签: angular mongoose


【解决方案1】:

首先,我将 http 的 url 从“/products/all 更改为 /allproducts,因为后者似乎与现有的 url “/products/:type 冲突。这并没有立即解决问题。然后我将函数从使用 Observable 更改为使用 Promise,如下所示,并收到了数据。 Observables 可能有一些怪癖。它在一种情况下有效,在另一种情况下无效。

@Injectable()
export class DataService {
  private products: Product[];  

  private productsUrl: string = "/api/allproducts";

  constructor(private http: Http) { 
    this.getProducts();
  } 

 public getProducts() {
    console.log("DataService.getProducts called");

    this.http.get(this.productsUrl)
      .toPromise()
      .then((response: Response) => {
        this.products = response.json()
        console.log("DataService.products = " + JSON.stringify(this.products));
      })
      .catch(this.handleError)    
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    相关资源
    最近更新 更多