Reputation: 25
when I console.log(response.data) it is undefined. Why is that?
I already tried res.send and res.json but they both just return undefined...
Should I be changing something in my client or in my server
Server side
//server js file
app.get("/updateCart", (req, res) => {
//get items from cart
dataServiceAuth.findUser("joe")
.then((user) => {
res.json(user[0]); //I know user[0] has a value I console.log it to the screen
})
.catch((err) => {
console.log("ERROR. Can't find user")
});
});
Client side
//client file
$("document").ready(function () {
fetch("/updateCart", {method: "GET"}).then(response => {
console.log(response.data);
});
});
Upvotes: 0
Views: 117
Reputation: 1509
app.get('/getItems' (req, res) => {
let x = [5, 4, 3, 2, 1];
res.send(x)
})
OR
app.get('/getItems' (req, res) => {
let x = [5, 4, 3, 2, 1];
res.json(x)
})
Client.js
var myItems;
fetch("/getItems", method: "GET").then(response => {
myItems = response.data;
});
Upvotes: 2