首页 > 解决方案 > Mongoose 5.3,this.getUpdate 不是函数

问题描述

当我尝试执行该update功能时,出现此错误。它发生在我添加时timestamps: true

我导航到错误,我在 mongoose >> lib 文件夹中的 schema.js 中调用了这个函数。

function _setTimestampsOnUpdate(next) {
  applyTimestampsToUpdate(createdAt, updatedAt, this.getUpdate(),
    this.options, true);
  applyTimestampsToChildren(this);
  next();
}

上述函数与getUpdatein一起使用schema.js。请帮我解决这个问题。

:: 架构 ::

const assetListSchema = new Schema({userId: {
  type: mongoose.Schema.Types.ObjectId,
  ref: 'User',
  required: true
  },
  name: String,
  config: {
  showAssetsLibrary: {
    type: Boolean,
    required: true,
    default: true
  },
  showAssetsDetails: {
    type: Boolean,
    required: true,
    default: true
  },
  aliasName: {
    type: String,
    maxlength: 75,
    default: ""
  }
}},{ timestamps: true});

:: 更新功能 ::

assetListSchema.methods.update = async function (data) {
try {
let asst = this;
data = _.pick(data, ['showAssetsLibrary', 
'showAssetsDetails','aliasName']);
if (_.isEmpty(data)) {
  emptyError();
}
return await AssetsHierarchy.findByIdAndUpdate(asst._id, {
  config: data
}, {
  new: true,
  fields: {
    "config": 1
  }
});
} catch (err) {
 throw new Error(err.message);
}
}

:: 错误 ::

类型错误:this.getUpdate 不是函数

在 model._setTimestampsOnUpdate (D:\inspectionApp\inspection-backend\node_modules\mongoose\lib\schema.js:873:56)

在 callMiddlewareFunction (D:\inspectionApp\inspection-backend\node_modules\kareem\index.js:427:23)

在下一个 (D:\inspectionApp\inspection-backend\node_modules\kareem\index.js:58:7)

在 Kareem.execPre (D:\inspectionApp\inspection-backend\node_modules\kareem\index.js:86:8)

在 Kareem.wrap (D:\inspectionApp\inspection-backend\node_modules\kareem\index.js:265:8)

在 model.$__update (D:\inspectionApp\inspection-backend\node_modules\kareem\index.js:339:11)

在 utils.promiseOrCallback.callback (D:\inspectionApp\inspection-backend\node_modules\mongoose\lib\helpers\model\applyHooks.js:80:30)

在 Promise (D:\inspectionApp\inspection-backend\node_modules\mongoose\lib\utils.js:246:5)

在新的承诺 ()

在 Object.promiseOrCallback (D:\inspectionApp\inspection-backend\node_modules\mongoose\lib\utils.js:245:10)

在 model.objToDecorate.(匿名函数) [作为更新] (D:\inspectionApp\inspection-backend\node_modules\mongoose\lib\helpers\model\applyHooks.js:79:20)

更新时 (D:\inspectionApp\inspection-backend\App\controllers\settings\asset-hierarchy\assetsHierarchyController.js:163:33)

在 Layer.handle [as handle_request] (D:\inspectionApp\inspection-backend\node_modules\express\lib\router\layer.js:95:5)

在下一个(D:\inspectionApp\inspection-backend\node_modules\express\lib\router\route.js:137:13)

在 Route.dispatch (D:\inspectionApp\inspection-backend\node_modules\express\lib\router\route.js:112:3)

在 Layer.handle [as handle_request] (D:\inspectionApp\inspection-backend\node_modules\express\lib\router\layer.js:95:5)

标签: javascriptmongoosemongoose-schema

解决方案


推荐阅读