配置文件验证失败:处理:需要路径`handle`

时间:2019-08-01 23:35:26

标签: express mongoose

我的目标是构建一个可以连接来自不同背景的不同专业人士的应用程序。我还使用猫鼬作为数据库。 我创建了一个profile.js,它将创建和更新配置文件。但是当我用邮递员测试时,出现以下错误: “配置文件验证失败:句柄:路径HANDLE是必需的。”

该如何解决该问题? 您的帮助将不胜感激。

const express = require('express');     // require express modules
const router  = express.Router();              // to use express router
const auth = require('../../middleware/auth');
const { check, validationResult } = require('express-validator');

const Profile = require('../../models/Profile');
const User = require('../../models/User');



//@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){
            return res.status(400).json({ msg:'No profile exists for this user'});
        }

        res.json(profile);
    } catch(err){
        console.error(err.message);
        res.status(500).send('Server error');
    }
});   //to create a route

//@route     POST api/profile
//@desc      Create or update users 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;

      //to build profile object
      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.status = status;
      if(githubusername) profileFields.githubusername = githubusername;
      if(skills){
          profileFields.skills = skills.split(',').map(skills => skills.trim());
      }

      //for the social object
      profileFields.social = {}
      if(youtube) profileFields.social.youtube = youtube;
      if(facebook) profileFields.social.facebook = facebook;
      if(twitter) profileFields.social.twitter = twitter;
      if(instagram) profileFields.social.instagram = instagram;
      if(linkedin) profileFields.social.linkedin = linkedin;

      try{
        let profile = await Profile.findOne({ user: req.user.id });

        if(profile){ //if there is a profile, we will update it
            
            profile = await Profile.findOneAndUpdate(
            { user: req.user.id}, 
            {$set: profileFields },
            {new: true}
        );

            return res.json(profile);           
        }

        //this will create profiles
        profile = new Profile(profileFields);

        await profile.save();
        res.json(profile);
      } catch(err){
          console.error(err.message);
          res.status(500).send('Server Error');
      }
    }
);

module.exports = router;

6 个答案:

答案 0 :(得分:1)

相同的代码,相同的问题。在概要文件模式中,有一个句柄字段设置为必填。我已将其注释掉,并且现在可以正常工作。

答案 1 :(得分:0)

与我制作的代码相同,您将发现轮廓模型中需要手柄,将其删除即可,您的代码将正常工作

答案 2 :(得分:0)

handle: {
        type: String,
        required: true,
        max: 40
}

将您的models / Profile.js中的代码更改为

handle: {
        type: String,
        required: false,
        max: 40
    },

答案 3 :(得分:0)

我遇到了同样的问题。但是,发生这种情况的原因是,在您的“配置文件”模式中,您可能将'handle'属性设置为必需的。因此,您必须给它,否则只需进行更改并将require值设置为false。如(require:false),希望您的问题会解决。

答案 4 :(得分:0)

发生这种情况是因为您的个人资料架构有一个句柄字段(属性),其中您有一个属性 required: true

转到配置文件架构文件并删除句柄属性(字段)或从句柄属性中删除 required: true<

示例

Profile.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const Profile = new Schema({
    ...,
    handle: {
        type: ...,
        required: true //remove this line
    }
    ...
});

答案 5 :(得分:-1)

使用

let const profileFields = {};
      profileFields.user = req.user.id

那是

let profileFields = {};
      profileFields.user = req.user.id

或者您可以使用

const profileFields = {};
    profileFields.user = req.user.id;
相关问题