无法在数字“ 1”上创建属性“ route”

时间:2018-07-05 09:35:02

标签: angularjs ionic2 ngx-restangular

使用RestAPI,我试图通过这些简单的代码从数据库中删除一条记录。

public class Parent {

    protected SomeClass someObject;

    public Parent() {
        this.someObject = new SomeClass();
    }
}



public class Derived extends Parent {

    public void printInvoked() {
        System.out.println(this.someObject.invoke());
    }
}


public class SomeClass {

    public String invoke() {
        return "SomeClass invoked";
    }
}

奇怪的是,文档已成功删除,但是我总是遇到以下错误,我的应用程序已关闭

import { Component } from '@angular/core';
import { RestangularModule, Restangular } from 'ngx-restangular'; 
import { HomePage } from '../pages/home/home';
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;
  constructor(private restangular: Restangular) {
      this.restangular.one('v1/my_products', '5b3621ce1dbaba5598cdbe08')
      .get()
      .subscribe((res: any) => {
          res.data.remove();
      })
  }
}

Package.json

    > ERROR TypeError: Cannot create property 'route' on number '1'
    at restangularizeBase (ngx-restangular.js:54)
    at restangularizeElem (ngx-restangular.js:184)
at SafeSubscriber.okCallback [as _next] (ngx-restangular.js:360)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
    at SafeSubscriber.next (Subscriber.js:185)
    at Subscriber._next (Subscriber.js:125)
    at Subscriber.next (Subscriber.js:89)
    at CatchSubscriber.Subscriber._next (Subscriber.js:125)
    at CatchSubscriber.Subscriber.next (Subscriber.js:89)
    at MapSubscriber._next (map.js:83)

与ngx-restangular@1.0.13-3.0.0相同的情况

怎么了?

2 个答案:

答案 0 :(得分:0)

我不知道这是否是最佳解决方案。但是,我刚刚找到了解决此问题的解决方案。

RestangularProvider.addResponseInterceptor((data, operation, what, url, response) => 
{
 switch (operation) {
  case 'post':
  case 'put':
  case 'remove':
    if (!data)
      return {};
  default: {
    if (typeof data === 'number') { return {} }
    if (typeof data === 'object') { return data }
  }

}
 });

答案 1 :(得分:0)

对@jlcj表示敬意

这是@jlcj答案的扩展,也是我到目前为止也发现的最佳解决方法。

我的应用程序正在运行ngx-restangular v5.0.0

Restangular期望对象是后端服务器的响应。因此,如果您希望后端服务器返回一个数字,则必须将数据包装在大括号{data}中作为对象。否则会出现错误“无法在1号上创建属性路由”。

RestangularProvider.addResponseInterceptor((data, operation, what, url, response) =>
{
  switch (operation) {
    case 'post':      
    case 'put':
    case 'remove':
    if (!data)
      return {};
    default: {
      if (typeof data === 'number') { return { data } }
      if (typeof data === 'object') { return data }
    }

  }
});