I am trying to make a page where customer could see all his added products. To get them from the database, I have written a post route where I'm selecting data by username. I have tested this request with Advanced REST client and it works.
routes.js
router.post('/myProducts', (req, res, next) => {
const username = req.body.username;
Product.getProductByUsername(username, (err, products) => {
if (err){
res.json({
success: false,
message: "Something went wrong!"
});
console.log(err);
}
else {
res.json({
success: true,
message: "List of products retrieved!",
products
});
}
});
});
Advanced REST client response
{
"success": true,
"message": "List of products retrieved!",
"products": [
{
"_id": "5adbac5e9eb619106ff65a39",
"name": "Car",
"price": 200,
"quantity": 1,
"username": "testUser",
"__v": 0
},
{
"_id": "5adc43049eb619106ff65a3a",
"name": "Lipstick",
"price": 2.3,
"quantity": 1,
"username": "testUser",
"__v": 0
},
{
"_id": "5adcf21c18fe1e13bc3b453d",
"name": "SuperCar",
"price": 2000,
"quantity": 1,
"username": "testUser",
"__v": 0
}
],
}
After that I have written a service to pass this data to frontend.
product.service.ts
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ProductService {
username: any;
constructor(private http: Http) { }
getProducts(username):any{
console.log(username);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/products/myProducts', username, {headers: headers})
.map(res => res.json());
}
}
And tried to use this service in my component to get the data from POST request. myproducts.component.ts
import { Component, OnInit } from '@angular/core';
import {ProductService} from '../../services/product.service'
@Component({
selector: 'app-myproducts',
templateUrl: './myproducts.component.html',
styleUrls: ['./myproducts.component.css']
})
export class MyproductsComponent implements OnInit {
userString: any;
user:any;
username: String;
products: Object;
constructor(private productService: ProductService) { }
ngOnInit() {
this.userString = localStorage.getItem('user');
this.user = JSON.parse(this.userString);
this.username = this.user.username;
console.log(this.username);
this.productService.getProducts(this.username).subscribe(myProducts => {
this.products = myProducts.products;
},
err => {
console.log(err);
return false;
});
}
}
I believe I've done something wrong here. Because I get 404 BAD request and then parse error because request expects response to be in json but gets it in html due to bad request. Could you help me figure out what I am doing wrong? I am pretty much self-taught and it's kinda complicated for me to understand all of this, yet. Thanks!
Your problem lies in how you are sending your username. That is a string while at server req.body is an object which is looking for a key named username which it does not find.
So, you can send an object instead:
return this.http.post('http://localhost:3000/products/myProducts', {username : username},
{headers: headers}).map(res => res.json());//^^^^ HERE ^^^^^^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With