从客户端传递给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()
。 对于普通Objects
, toString()
的默认实现是返回"[Object object]"
,这是愚蠢的,但也很通用。
如果您想查看对象的完整结构,请将其stringify
:
console.log(JSON.stringify(req.params.photoreference));
您可以要求JSON.stringify
渲染一个人类可读的版本,使用2个空格作为缩进:
console.log(JSON.stringify(req.params.photoreference, null, 2))
- 使用uglify将JavaScript打印到JavaScript
- TypeScript编译器包含TypeScript定义中不同名称的模块
- 在打字稿中访问Nodejsstream的highWaterMark属性给出:“Property'_readableState'不存在”错误
- 要求使用TypeScript,SystemJS和Electron的nodejs“child_process”
- “ng2-CKEditor”节点模块不能使用打印机
- 添加服务器端代码到angular-cli项目与节点types冲突
- 在TypeScript中声明JavaScript扩展函数
- Guid类在node.js中
- 当“不要做”不是一个选项时,如何将Typescript命名空间与外部模块混合在一起