当我console.log(response.data)时,它是未定义的。为什么会这样?
我已经尝试过res.send和res.json,但是它们都返回undefined ...
我应该在客户端或服务器中进行更改
服务器端
//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 file
$("document").ready(function () {
fetch("/updateCart", {method: "GET"}).then(response => {
console.log(response.data);
});
});
答案 0 :(得分:2)
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;
});