简体   繁体   English

无法从 nodejs 服务器获取 res.json() 值

[英]Not able to get the res.json() value from nodejs server

I am using react.js as frontend and nodejs for the backend.我使用 react.js 作为前端,使用 nodejs 作为后端。 My client-side code is我的客户端代码是

 export const updatePaymentDetails = (userId, token, paymentDetails) => { return fetch(`${API}/user/${userId}`, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json", Authorization: `Bearer ${token}` }, body: JSON.stringify(paymentDetails) }).then(response => { console.log(response); return response.json(); }).catch(err => console.log(err)); };

And My server-side code is我的服务器端代码是

 exports.updateUser = (req, res) => { User.findByIdAndUpdate( {_id: req.profile._id}, {$set: req.body}, {new: true, useFindAndModify: false}, (err, user) => { if(err) { return res.status(400).json({ error: "You are not authorized to update this user" }); } user.salt = undefined; user.encry_password = undefined; user.createdAt = undefined; user.updatedAt = undefined; console.log(user); return res.json(user); } ); };

Front-end output前端 output 在此处输入图像描述

In the server-side code, you can see that I am returning the res.json.在服务器端代码中,您可以看到我正在返回 res.json。 but On the client-side, I am not getting the value that I have returned from the server.但在客户端,我没有得到从服务器返回的值。

Please, can anyone help me?请问,谁能帮帮我?

You need to add one more then().您需要再添加一个 then()。 when you call response.json() it also returns a promise apply a then call when you return response.json()当您调用 response.json() 它还返回 promise 当您返回 response.json() 时应用 then 调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM