字段"/>
使用mongodb向nodeJs模型添加新字段
我在nodeJs api中有一个用户模型,并且使用mongo db和Angular作为FrontEnd框架,我想向我的用户模型添加一个新字段,我所做的是添加名为“ municipalityDateChange”的字段,并尝试从angular发送一个http请求到nodeApi并记录响应,用户确实包含新字段,默认情况下为空!
import mongoose, {
Schema
} from 'mongoose'
import mongooseDelete from 'mongoose-delete'
import bcrypt from 'bcrypt'
import crypto from 'crypto'
const userSchema = new Schema({
firstName: {
type: String
},
lastName: {
type: String
},
phone: {
type: Number
},
email: {
type: String
},
hashedPassword: {
type: String
},
address: {
type: String
},
city: {
type: mongoose.Schema.Types.ObjectId,
ref: 'City'
},
municipalityDateChange: {
type: Date,
default: null
},
service: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Service'
},
postalCode: {
type: String
},
gender: {
type: String
},
age: {
type: Number
},
birthDate: {
type: Date
},
profession: {
type: String
},
tokens: [{
token: {
type: String,
// required: true
}
}],
token: {
type: String
},
type: {
type: String,
enum: ['superAdmin', 'president', 'chefService', 'secretaireGeneral', 'conseillerMunicipal', 'citoyen'],
default: 'citoyen'
},
activated: {
type: Boolean,
default: false
},
suspended: {
type: Boolean,
default: false
},
avatar: {
type: mongoose.Schema.Types.ObjectId,
ref: 'File'
},
confirmationCode: {
type: Number
},
resetPasswordCode: {
type: Number
},
codeSent: {
type: Number,
default: 0
},
allInfo: {
type: Boolean,
default: false
},
propositions: [{
evaluation: {
type: String,
enum: ['like', 'dislike']
},
proposition: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Proposition'
}
}],
commissions: [{
type: String
}]
}, {
timestamps: true
})
userSchema.virtual('password').set(function (password) {
this.hashedPassword = bcrypt.hashSync(password, bcrypt.genSaltSync(10))
})
function calculateAge(birthDate, otherDate) {
birthDate = new Date(birthDate);
otherDate = new Date(otherDate);
var years = (otherDate.getFullYear() - birthDate.getFullYear())
if (otherDate.getMonth() < birthDate.getMonth() ||
otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) {
years--;
}
return years
}
userSchema.pre('save', function (next) {
this.age = calculateAge(this.birthDate, new Date())
next()
})
userSchema.methods = {
comparePassword(candidatePassword) {
return bcryptpareSync(candidatePassword, this.hashedPassword)
}
}
userSchema.methods.generateAuthToken = async function () {
// Generate an auth token for the user
const user = this
const token = crypto
.createHash('sha256')
.update(crypto.randomBytes(48).toString('hex'))
.digest('hex')
user.tokens = user.tokens.concat({
token
})
await user.save()
return token
}
userSchema.plugin(mongooseDelete, {
overrideMethods: 'all',
deletedAt: true,
deletedBy: true
})
export default mongoose.model('User', userSchema)
*用户控制器*
export async function getOne (req, res) {
try {
const user = await User.findById({
_id: req.params.id
})
.populate('avatar')
// .populate('city')
// .populate('service')
.select('firstName lastName type avatar phone email gender age postalCode address profession activated suspended municipalityDateChange')
.lean()
.exec()
user.avatar ? user.avatar = user.avatar.filePath : delete user.avatar
return res.json(user)
} catch (error) {
console.log(error)
return res.status(500).end()
}
}
ts
this.http.get('api/user').subscribe(user => {
console.log("current user", user)
this.currentUser = user;
})
日志未显示用户已提交新文件!
在稀疏情况下我该怎么办?更新我的所有收藏文档,或者有其他方法可以在模型中设置新文件?
回答如下:我认为您需要将municipalityDateChange
的字段键值更改为:更多推荐
使用mongodb向nodeJs模型添加新字段
发布评论