我正在使用nodejs express和postman构建和API,我尝试在postman中发送请求,但在控制台中出现以下错误:
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.
这是我的代码在我的API中,我有三个路线一个得到一个配置文件,另一个创建/更新配置文件,另一个得到所有配置文件:
profile.js:
const express = require('express');
const router = express.Router();
const auth = require('../../middleware/auth');
const Profile = require('../../models/Profile');
const User = require('../../models/User');
const {check, validationResult} = require('express-validator/');
const { route } = require('./users');
//@route GET api/profile/me
//@desc Get current users profile
//@access private
router.get('/me', auth, async(req, res)=> {
try{
const profile = await Profile.findOne({user: req.user.id}).populate('user' ['name', 'avatar']);
if(!profile){
res.status(400).json({ msg: 'There is no profile for this user' });
}
res.json(profile);
}catch(err){
console.error(err.message);
res.status(500).send('Server error');
}
});
//@route POST api/profile/
//@desc Create or update a user profile
//@access private
router.post(
"/",
[
auth,
[
check("status", "Status is required").not().isEmpty(),
check('skills', 'Skills is required').not().isEmpty()
]
],
async (req, res) => {
const errors = validationResult(req);
if(!errors.isEmpty()){
return res.status(400).json({ errors: errors.array() });
}
const{
company,
website,
location,
bio,
status,
githubusername,
skills,
youtube,
facebook,
twitter,
instagram,
linkedin
} = req.body;
//Build profile objetc
const profileFields = {};
profileFields.user = req.user.id;
if(company) profileFields.company = company;
if(website) profileFields.website = website;
if(location) profileFields.location = location;
if(bio) profileFields.bio = bio;
if(status) profileFields.bio;
if(status) profileFields.status = status;
if(githubusername) profileFields.githubusername = githubusername;
if(skills){
profileFields.skills = skills.split(',').map(skill =>skill.trim());
}
//Build social Object
profileFields.social = {}
if(youtube) profileFields.social.youtube = youtube;
if(twitter) profileFields.social.twitter = twitter;
if(facebook) profileFields.social.facebook = facebook;
if(linkedin) profileFields.social.linkedin = linkedin;
if(instagram) profileFields.social.instagram = instagram;
try{
let profile = await Profile.findOne({ user:req.user.id });
if(profile){
//Update existing profile
profile = await Profile.findOneAndUpdate(
{ user:req.user.id },
{ $set:profileFields },
{ new:true }
);
return res.json(profile);
}
//Create
profile = new Profile(profileFields);
await profile.save();
res.json(profile);
}catch(err){
console.error(err.message);
res.status(500).send('Server error');
}
}
);
//@route GET api/profile/
//@desc Get all profiles
//@access public
router.get('/', async(req,res)=>{
try {
const profiles = await Profile.find().populate('user', ['name', 'avatar']);
res.json(profiles);
} catch (err) {
console.error(err.message);
res.status(500).send('Server error');
}
})
module.exports = router;
因此,该过程如下所示,使用我的路由,我首先创建一个用户,然后登录该用户,并接收一个令牌,然后使用该令牌,我尝试通过路由获取已登录用户的数据
http://localhost:5000/api/profile/me
我得到消息There is no profile for this user
,因为我还没有任何创建的配置文件.
所以我做了一个创建和更新用户配置文件的路径。
http://localhost:5000/api/profile
其中,此路由的标头是值为application/json
的Content-type
和值为the token of the logged in user
的x-auth-token
。
但是当我在 Postman 中发送请求时,我得到错误Error: connect ECONNREFUSED 127.0.0.1:5000
。
这个错误是否来自我的代码?
1条答案
按热度按时间vc6uscn91#
在没有可用的配置文件时发送错误响应后,您缺少
return
语句: