Angular 2:带ID的HTTP GET请求

时间:2017-08-31 18:02:33

标签: angular angular-http

我有一个特定对象的http get请求。因此,我在请求中传递ID。在服务器端,值现在是$ {id},但不是实际传递的数字。我究竟做错了什么?在另一个应用程序中它正在工作..

服务

getLeague(id){
    return this.http.get('api/leagues/${id}').map(res => res.json());
};

服务器侧

router.route('/:league_id')
     .get(function(req,res){
         League.findOne({id: req.params.league_id})
     .exec(function(err,docs){
         if(err)
         res.send(err);
     res.json(docs);
  })});

在我的测试代码中,我只是简单地调用,例如getLeague(5),但是,我收到错误(似乎ID不是保存实际值而只是变量名称):

message: "Cast to number failed for value "${id}" at path "id" for model "League"", name: "CastError", stringValue: ""${id}"

谢谢!

3 个答案:

答案 0 :(得分:3)

您没有使用字符串插值(Template Literals)。不需要单引号,而是需要在您的网址周围打勾。

答案 1 :(得分:1)

您尚未将{{id}}传递给您的链接。您对后端的请求包括" / $ {id}"部分。要解决Typescript中字符串中的变量,您应该使用刻度来包装字符串以使用插值,即

#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>

Servo servoOne;

String sharp="";
int piso=0;
int lima=0;
int sampu=0;

String input="";
String remlast = "";

LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);//RS,EN,D4,D5,D6,D7


const byte Rows= 4; //number of rows on the keypad i.e. 4
const byte Cols= 3; //number of columns on the keypad i,e, 3

//we will definne the key map as on the key pad:

char keymap[Rows][Cols]={
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rPins[Rows]= {3,4,5,6}; //Rows 0 to 3
byte cPins[Cols]= {7,8,9}; //Columns 0 to 2

Keypad kpd= Keypad(makeKeymap(keymap), rPins, cPins, Rows, Cols);

void setup() {
  // put your setup code here, to run once:
lcd.begin(20,4);
servoOne.attach(10);
}

void loop() {
    char key2 = kpd.getKey();


  if (key2 != NO_KEY)
     { 
       lcd.print(key2);

       if (sharp == "")
       {
          input+=key2;
          remlast = input;
          remlast.replace("*","");
            remlast.replace("#","");
            piso = remlast.toInt();
       }
       else if (sharp == "five")
       {
          input+=key2;
          remlast = input;
          remlast.replace("*","");
            remlast.replace("#","");
            lima = remlast.toInt();
       }
       else if (sharp == "ten")
       {
          input+=key2;
          remlast = input;
          remlast.replace("*","");
            remlast.replace("#","");
            sampu = remlast.toInt();
       }

      if(key2=='*' && sharp!=NULL)
      {
        lcd.rightToLeft();
        sharp="";
        piso=0;
        lima=0;
        sampu=0;
        input="";
        remlast="";
      }  

      if (sharp=="ten" && key2=='#')
      {
        sharp = "out";
        lcd.clear();
        lcd.print(piso);
        lcd.print(lima);
        lcd.print(sampu);
        servoOne();
      }

     else if (sharp=="five" && key2=='#')
       {
          lcd.clear();
          lcd.print("10-peso=");
          lcd.setCursor(0,1);
          lcd.print("(*)Erase  (#)Enter");
          lcd.setCursor(8,0);  
          sharp="ten";  
          input = 0;      
       }

     else if (key2=='#')
      {
        lcd.clear();
        lcd.print("5-peso=");
        lcd.setCursor(0,1);
        lcd.print("(*)Erase  (#)Enter");
        lcd.setCursor(7,0); 
        sharp="five";
        input = 0;

      }
      if (key2=='*')
      {
        lcd.clear();
        lcd.print("1-peso=");
        lcd.setCursor(0,1);
        lcd.print("(*)Erase  (#)Enter");
        lcd.setCursor(7,0); 
      }

      }
   }

//--------------------SERVO ONE--------------------//
void servoOne()
{
  servoOne.write(70);
  delay(10);
  while(piso>0)
  {
    int x = piso;
    while(x>0)
    {
      servoOne.write(170);
      delay(200);
      servoOne.write(40);
      delay(200);
      x--;
    }
  }
}

答案 2 :(得分:0)

正如本URL

中的语法所述
`api/leagues/${id}`

使用反向勾选```应该解决这个问题