Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String turned to Object when passed from client to nodeJS

I'm using yeoman angular-fullstack to generate my project. So the client is angularJs (typeScript) and the backend is nodeJs. The problem is I got a variable and when I print it to the console, I get a very long string, (if u need to know its a photo_reference from googleplacesapi). And when I pass it to the with a http.get to the nodeJS api, and print it to the log I get the response Object 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);
like image 405
Jedi Schmedi Avatar asked Mar 07 '16 21:03

Jedi Schmedi


People also ask

How do you pass an object in node JS?

js – Passing Objects As Parameters. Due to the fact that you cannot access objects declared inside another function, you may need to grant other functions access to it. One way to do this in Node. js is by passing the object as a parameter to the function you'd like to use it in.

How do I pass an object to an EJS file?

It is possible to access JS variable in . ejs file. You just need to pass the JS object as second parameter of res. render() method.

How do I print a JSON object in node JS?

console. dir with the depth argument set will do the trick. This will print JSON objects to any arbitrary depth. Depth:null means print every level.


1 Answers

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

pass the photo.photo_reference to the function

for (var photo of response.data.result.photos) {
    this.getImages(photo.photo_reference);
}
like image 132
eltonkamami Avatar answered Sep 29 '22 04:09

eltonkamami