2016-03-07 33 views
5

我使用yeoman angular-fullstack生成我的項目。 所以客戶端是angularJs(typeScript),後端是nodeJs。 問題是我有一個變量,當我打印到控制檯時,我得到一個很長的字符串,(如果你需要知道它從googleplacesapi的photo_reference)。 當我將它傳遞給帶有http.get的nodeJS api,並將其打印到日誌中時,我得到響應Object對象。從客戶端傳遞到nodeJS時,字符串轉向對象

MainController

for (var photo of response.data.result.photos) { 
     this.getImages(photo); 
     console.log(photo.photo_reference); 
    } 
    getImages(photo_reference: string): void{ 
    this.$http.get('/api/image/' + photo_reference).then((response) => { 

    }); 
    } 

的NodeJS

export function show(req, res) { 

    console.log("photoreference:" + req.params.photoreference); 
+1

@mmm好像打字稿 – eltonkamami

+0

出於好奇(不是溶液) ,如果你顯式轉換爲字符串,它是否工作? – ale10ander

+0

記錄參數,而不將它連接到一個字符串以查看傳入的值'console.log(req.params.photoreference);' – eltonkamami

回答

3

您將錯誤的值傳遞給getImages函數。 由於傳遞給getImages參數具有photo_reference屬性它是一個對象,以便記錄是正確

傳遞photo.photo_reference給函數

for (var photo of response.data.result.photos) { 
    this.getImages(photo.photo_reference); 
} 
+0

謝謝,現在這是一個愚蠢的misstake。我在譴責這個晚上的時間。 :P –

+0

@JediSchmedi第二雙眼睛總是幫助:) – eltonkamami

3

console.log將調用在傳遞給它的任何對象.toString()。對於普通ObjectstoString()的默認實現是返回"[Object object]",這是愚蠢的,但也很通用。

如果你想看到完整結構的對象,stringify它:

console.log(JSON.stringify(req.params.photoreference)); 

你可以問JSON.stringify來呈現人類可讀的版本,使用2個空格進行縮進:

console.log(JSON.stringify(req.params.photoreference, null, 2)) 
相關問題