476192 (8) [Avatar] Offline
#1
Hello Clive and everyone,

First of all, thank you so much for the updates! I am enjoying this book and I am currently trying to work my way through CH 12.

When I run the code from CH 12 there are a couple of bugs/errors:

The first one is that the home page is only returning one location, instead of the 3 that I have in my database. I have changed the base coordinates so that they are closer to my desired destinations, so I am not sure why I'm not getting an array of locations returned.

Also, whenever I attempt to leave a review (after having logged in) I am getting this error from the ```handleError``` function in ```loc8r-data.service.ts``` in my console:


ERROR Error: "Uncaught (in promise): Http failure response for http://localhost:3000/api/locations/5be499a74a8cfaf7ec3656df/reviews: 404 Not Found"


Thinking that it may have something to do with the author not being referenced to the User collection, I added this to my ```reviewSchema``` to no avail:

type: mongoose.Schema.Types.ObjectId, ref: 'User',


I checked all of the endpoints and everything looks about right as far as I can tell.

I also added an additional route to ```app-routing.module.ts```, also to no avail:

  {
    path: 'locations/:locationId/reviews',
    component: DetailsPageComponent
  }



Lastly, I tried to send a POST request from Postman with these settings:

POST localhost:3000/api/locations/5be499a74a8cfaf7ec3656df/reviews


Body -> x-www-form-urlencoded


..with these inputs:

author, rating, & reviewText

....and the response is:

{
    "message": "User not found"
}



...so I added these keys to the headers:

Content-Type -> application/x-www-form-urlencoded

userId + token + loc8r-token (w/values)


I am still getting a 404/User not found in Postman, along with the response error in the browser.

What could be wrong?


Thanks so much!


Juliette
Clive.Harber (11) [Avatar] Offline
#2
Hi Juliette

Thanks for reading the book.

From your description, I'm not sure where the problem lies.

Would you be able to either push it up to GitHub somewhere and send me the URL, or package up your project and send it to me and I'll take a look.

Thanks

Clive
476192 (8) [Avatar] Offline
#3
Hi Clive,

Thanks for getting back to me and thank you for offering to take the time to take a look at my code.
I have arranged the structure of the directories is a bit differently (according to what I'm used to), but most everything else (code-wise) is the same as what has been provided via the course repo (CH 12).

Here is my repo:

https://github.com/firebuggirl/coffee-house-locations

Cheerssmilie

Juliette
Clive.Harber (11) [Avatar] Offline
#4
Some things of note:

In your server.js file, you're missing the CORS setting for the Authorization header

Line 50 should read:
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
This is in Chapter 11 (listing 11.10)

The next issue is that you're not adding the auth context to the createReviews routing setup, so in app_api/routes/index.js, line 26, you need to add auth to the line, like so:

  .post(auth, ctrlReviews.reviewsCreate); 


See Chapter 11 (listing 11.16)

This passes the logged in user to getAuthor, and prevents the 404

Lastly, once those are in place, it looks like there is a bug in the text where the page isn't updating when the review submission is successful, to fix that change the location-details.component.ts onReviewSubmit function to:

public onReviewSubmit(): void {
    this.formError = '';
    this.newReview.author = this.getUsername();
    if (this.formIsValid()) {
      this.loc8rDataService.addReviewByLocationId(this.location._id, this.newReview)
        .then((review: Review) => {
          let newReviews = this.location.reviews.slice(0);
          newReviews.unshift(review);
          this.location.reviews = newReviews;
          this.resetAndHideReviewForm();
        });
    } else {
      this.formError = 'All fields requried, please try again';
    }
  }


Then it should now work.
476192 (8) [Avatar] Offline
#5
Hi Clive,

Thank you for taking the time to explain what I was missing and why! Everything is working as expected now. I'm going to have to go back and read the last few chapters again to re-absorb everything.

Cheers and Happy Tuesday smilie

Juliette