async update() {
const { ctx } = this;
try {
let results;
await ctx.model.Role.findByIdAndUpdate(
'6445f82c6194135a90fdde72',
{ name: 'test-hello' }
).then(res => {
results = true;
}).catch(err => {
results = false;
});
return results;
} catch (err) {
return JSON.stringify(err);
}
}
模型model文件未定义好,部分字段无法更新
数据库操作的字段必须在模型中定义,否则无法更新相应的字段
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
// 连接数据库,创建Schema(模式)对象
const UserSchema = new Schema({
name: {
type: String,
},
id: {
type: String,
},
hobby: {
type: String,
},
updateDate: {
type: String,
},
});
// 通过Schema创建Model,Model 代表的是数据库中的集合,通过Model才能对数据库进行操作
// 第一个参数模型
// 第二个参数 数据
// 第三个参数 操作的数据集合(表)
// 映射
return mongoose.model('Role', UserSchema, 'user');
};