Cannot create property 'route' on number '1'

3.5k Views Asked by At

With RestAPI, I'm trying to remove one record from my database through these simple code.

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();
      })
  }
}

The weird, the document has been successfully removed, but I always get this following error and my app is down

    > 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)

Package.json

{
  "dependencies": {
    "@angular/animations": "5.0.1",
    "@angular/common": "5.0.1",
    "@angular/compiler": "5.0.1",
    "@angular/compiler-cli": "5.0.1",
    "@angular/core": "5.0.1",
    "@angular/forms": "5.0.1",
    "@angular/http": "5.0.1",
    "@angular/platform-browser": "5.0.1",
    "@angular/platform-browser-dynamic": "5.0.1",
    "@ionic-native/core": "4.8.0",
    "@ionic-native/splash-screen": "4.8.0",
    "@ionic-native/status-bar": "4.8.0",
    "@ionic/storage": "2.1.3",
    "core-js": "^2.5.7",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "ngx-restangular": "2.0.1",
    "rxjs": "5.4.3",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.1.10",
    "typescript": "~2.6.2"
  }
}

Same case with [email protected] - 3.0.0

what's wrong?

2

There are 2 best solutions below

0
Léon Jiresse On

I don't know if is it the best solution. But, I've just found a solution to fix the issue.

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 }
  }

}
 });
0
omostan On

Kudos to @jlcj

This is an extension of @jlcj's answer and the best fix I have also found so far.

My application is running ngx-restangular v5.0.0

Restangular is expecting an object as response from the back-end server. Therefore, if you are expecting a number to be returned from the back-end server, you have to wrap the data in the curly braces { data } as an object. Otherwise the error "cannot create property route on number 1" occurs.

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 }
    }

  }
});