-1

I am trying to implement MEAN app for which I made node server

app.get("/posts",(req,res)=>{
posts=[{"title":"a","context":"b"},{"title":"c","context":"d"}] 
res.send(posts); // tried even with res.status(200).json(posts)   
});

when it checked with api tester it works well output snapshot output snapshot with apitester

when i try to access with angular services

getposts()
      {
        var url='http://localhost:3000/posts';
        this.http.get<post[]>(url).subscribe(data=>this.posts=data);   
        console.log(this.posts);
        return this.posts;

      }

when i do console.log(posts) it returning []

can someone please help i am struggling from last 2 days?

  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – CertainPerformance Apr 18 '20 at 02:27
  • no can u please write answer for this question – Arvind Ravva Apr 18 '20 at 04:17

1 Answers1

-1

As you have not added the screenshot from the Browser's Inspector => Network Tab, I am guessing this answer will help you in case my guess is right.

Try setting the below in your front-end project (Angular in your case):

Request Headers

Try setting the below in your back-end project (Node in your case):

Response Headers

Also, verify your updates as in the below screengrab in the Browser's Inspector (Chrome in my case):

Chrome's Inspector Screenshot

Please Note: - The mentioned response headers have special meaning, so kindly look for more info with a simple Google search. Use these settings during your development phase on localhost.

Santhosh John
  • 648
  • 5
  • 14
  • For whoever has down voted this answer, I respect their view, but may I also know the reason please? I have given my time and effort to guess the actual issue and to answer it provided that, the same was mentioned in the answer itself that the question is not fully complete. – Santhosh John Apr 18 '20 at 15:00