String jumped to object when passing from client to nodeJS

I use yoman angular -fullstack to create my project. So the client is angularJs (typeScript) and the backend is nodeJs. The problem is that I have a variable, and when I print it on the console, I get a very long line (if you need to know its photo_reference from googleplacesapi). And when I pass it from http.get to nodeJS api and print it in the log, I get the Object of the response 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); 
+5
source share
2 answers

You are passing the wrong value to the getImages function. since the argument passed to getImages has the photo_reference property, this is an object, so logging is correct.

pass photo.photo_reference function

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

console.log will reference .toString() on any objects that you pass to it. The default implementation of toString() for simple Objects is to return "[Object object]" , which is silly, but also joyful.

If you want to see the full structure of an object, stringify it:

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

You can request JSON.stringify render the human-readable version using 2 spaces for indentation:

 console.log(JSON.stringify(req.params.photoreference, null, 2)) 
+3
source

Source: https://habr.com/ru/post/1244612/


All Articles