我正在上一门Udemy
课程,并且尝试推广体验领域。当我这样做时,我得到了错误。我检查了该字段是否确实存在于数据库中并且确实存在。也没有拼写错误,并且我也无法取消移动JSON
对象。我按照教程中的每个步骤进行操作,但他在视频中做到了,但我却做不到。
JSON我正在尝试在体验字段上push
:
{ "title":"developer",
"company":"mangoo",
"location":"karaikal",
"from":"01-12-2018",
"current":"true",
"description":"developed android app for the firm"
}
https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js
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(配置文件):
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.net',
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],
'experience.company': [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
}
答案 0 :(得分:0)
您忘记添加await
关键字:
const profile = await Profile.findOne({user:req.user.id})