从客户端传递给nodeJS时,string转换为Object

我正在使用yeoman angular-fullstack来生成我的项目。 所以客户端是angularJs(typeScript),后端是nodeJs。 问题是我有一个variables,当我打印到控制台,我得到一个非常长的string,(如果你需要知道它从googleplacesapi photo_reference)。 而当我通过它的http.get传递给nodeJS api,并将其打印到日志中,我得到响应Object对象。

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

您传递错误的值给getImages函数。 因为传递给getImages的参数具有photo_reference属性,所以它是一个对象,所以日志logging是正确的

photo.photo_reference传递给函数

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

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