在尝试取消移动json对象时无法读取未定义的属性'unshift'

编程入门 行业动态 更新时间:2024-10-10 10:23:35

在尝试取消移动json对象时无法读取未定义的<a href=https://www.elefans.com/category/jswz/34/1771415.html style=属性'unshift'"/>

在尝试取消移动json对象时无法读取未定义的属性'unshift'

我正在学习Udemy课程,但我尝试推动体验领域。当我这样做时,我得到了错误。我检查了该字段是否确实存在于数据库中并且确实存在。也没有拼写错误,并且我也无法取消移动JSON对象。我按照教程中的每个步骤进行操作,但是他在视频中可以这样做,但我却没有做到。

JSON,我正在尝试在体验字段push

{   "title":"developer",
    "company":"mangoo",
    "location":"karaikal",
    "from":"01-12-2018",
    "current":"true",
    "description":"developed android app for the firm" 
}

router.put('/experience',[
    auth,
    [check('title','Title is empty').not().isEmpty(),
    check('company',"Company name is empty").not().isEmpty(),
    check('from','from date is empty').not().isEmpty()]],
    async (req,res)=>{
        const errors=validationResult(req)
        if(!errors.isEmpty()){
            return res.status(400).json({errors:errors.array()})
        }

        const {title,
            company,
            location,
            from,
            to,
            current,
            description
        } = req.body

        const newExp={
            title,
            company,
            location,
            from,
            to,
            current,
            description
        }

        try {
            const profile = Profile.findOne({user:req.user.id})
            console.log(profile)
            profile.experience.unshift(newExp)
            await profile.save()
            req.json(profile)
        } catch (error) {
            console.error(error.message);
            res.status(500).json('server error')


        }

})

模式:

const mongoose=require('mongoose')

const ProfileSchema= new mongoose.Schema({
    user:{
        type: mongoose.Schema.Types.ObjectId,
        ref:'user'    
    },
    company:{
        type:String
    },
    website:{
        type:String
    },
    location:{
        type:String
    },
    status:{
        type:String,
        required:true
    },
    skills:{
        type:[String],
        required:true
    },
    bio:{
        type:String
    },
    gitid:{
        type:String
    },
    experience:[
        {
            title:{
                type:String,
                required:true
            },
            company:{
                type:String,
                requiredt:true
            },
            location:{
                type:String
            },
            from:{
                type:Date,
                required:true
            },
            to:{
                type:Date,
                default: Date.now
            },
            current:{
                type:Boolean,
                default:false
            },
            description:{
                type:String
            }
        }
    ],
    education:[
        {
            school:{
                type:String,
                require:true
            },
            degree:{
                type:String,
                required:true
            },
            fieldofstudy:{
                type:String,
                required:true
            },
            from:{
                type:Date,
                required:true
            },
            to:{
                type:Date,
                default: Date.now
            },
            current:{
                type:Boolean,
                default: false
            },
            description:{
                type:String
            }
        }
    ],
    social:{
        youtube:{
            type:String
        },
        twitter:{
            type:String
        },
        facebook:{
            type:String
        },
        linkedin:{
            type:String
        },
        instagram:{
            type:String
        }
    },
    date:{
        type:Date,
        default:Date.now
    }
})

module.exports=Profile=mongoose.model('profile',ProfileSchema)

console.log(profile):

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map {}, _posts: Map {} },
  _executionCount: 0,
  mongooseCollection: NativeCollection {
    collection: Collection { s: [Object] },
    Promise: [Function: Promise],
    _closed: false,
    opts: {
      bufferCommands: true,
      capped: false,
      autoCreate: undefined,
      Promise: [Function: Promise],
      '$wasForceClosed': undefined
    },
    name: 'profiles',
    collectionName: 'profiles',
    conn: NativeConnection {
      base: [Mongoose],
      collections: [Object],
      models: [Object],
      config: [Object],
      replica: false,
      options: null,
      otherDbs: [],
      relatedDbs: {},
      states: [Object: null prototype],
      _readyState: 1,
      _closeCalled: false,
      _hasOpened: true,
      plugins: [],
      id: 0,
      _listening: false,
      _connectionOptions: [Object],
      client: [MongoClient],
      '$initialConnection': [Promise],
      name: 'test',
      host: 'mern-shard-00-00-jakyl.mongodb',
      port: 27017,
      user: 'jaga',
      pass: 'jaggu123',
      db: [Db]
    },
    queue: [],
    buffer: false,
    emitter: EventEmitter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      [Symbol(kCapture)]: false
    }
  },
  model: Model { profile },
  schema: Schema {
    obj: {
      user: [Object],
      company: [Object],
      website: [Object],
      location: [Object],
      status: [Object],
      skills: [Object],
      bio: [Object],
      gitid: [Object],
      experience: [Array],
      education: [Array],
      social: [Object],
      date: [Object]
    },
    paths: {
      user: [ObjectId],
      company: [SchemaString],
      website: [SchemaString],
      location: [SchemaString],
      status: [SchemaString],
      skills: [SchemaArray],
      bio: [SchemaString],
      gitid: [SchemaString],
      experience: [DocumentArrayPath],
      education: [DocumentArrayPath],
      'social.youtube': [SchemaString],
      'social.twitter': [SchemaString],
      'social.facebook': [SchemaString],
      'social.linkedin': [SchemaString],
      'social.instagram': [SchemaString],
      date: [SchemaDate],
      _id: [ObjectId],
      __v: [SchemaNumber]
    },
    aliases: {},
    subpaths: {
      'skills.$': [SchemaString],
      'experience.title': [SchemaString],
      'experiencepany': [SchemaString],
      'experience.location': [SchemaString],
      'experience.from': [SchemaDate],
      'experience.to': [SchemaDate],
      'experience.current': [SchemaBoolean],
      'experience.description': [SchemaString],
      'experience._id': [ObjectId],
      'education.school': [SchemaString],
      'education.degree': [SchemaString],
      'education.fieldofstudy': [SchemaString],
      'education.from': [SchemaDate],
      'education.to': [SchemaDate],
      'education.current': [SchemaBoolean],
      'education.description': [SchemaString],
      'education._id': [ObjectId]
    },
    virtuals: { id: [VirtualType] },
    singleNestedPaths: {},
    nested: { social: true },
    inherits: {},
    callQueue: [],
    _indexes: [],
    methods: {},
    methodOptions: {},
    statics: {},
    tree: {
      user: [Object],
      company: [Object],
      website: [Object],
      location: [Object],
      status: [Object],
      skills: [Object],
      bio: [Object],
      gitid: [Object],
      experience: [Array],
      education: [Array],
      social: [Object],
      date: [Object],
      _id: [Object],
      __v: [Function: Number],
      id: [VirtualType]
    },
    query: {},
    childSchemas: [ [Object], [Object] ],
    plugins: [ [Object], [Object], [Object], [Object], [Object] ],
    '$id': 2,
    s: { hooks: [Kareem] },
    _userProvidedOptions: {},
    options: {
      typePojoToMixed: true,
      typeKey: 'type',
      id: true,
      noVirtualId: false,
      _id: true,
      noId: false,
      validateBeforeSave: true,
      read: null,
      shardKey: null,
      autoIndex: null,
      minimize: true,
      discriminatorKey: '__t',
      versionKey: '__v',
      capped: false,
      bufferCommands: true,
      strict: true,
      pluralization: true
    },
    '$globalPluginsApplied': true
  },
  op: 'findOneAndUpdate',
  options: {},
  _conditions: { user: '5ebd21d78db2ec4370193636' },
  _fields: undefined,
  _update: { '$push': { experience: [Object] } },
  _path: undefined,
  _distinct: undefined,
  _collection: NodeCollection {
    collection: NativeCollection {
      collection: [Collection],
      Promise: [Function: Promise],
      _closed: false,
      opts: [Object],
      name: 'profiles',
      collectionName: 'profiles',
      conn: [NativeConnection],
      queue: [],
      buffer: false,
      emitter: [EventEmitter]
    },
    collectionName: 'profiles'
  },
  _traceFunction: undefined,
  '$useProjection': true
}
回答如下:

您忘记添加await关键字:

const profile = await Profile.findOne({user:req.user.id})

更多推荐

在尝试取消移动json对象时无法读取未定义的属性'unshift'

本文发布于:2024-05-07 15:32:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   未定义   json   unshift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!