When exactly does Mongoose's .pre('init') get called?

老子叫甜甜 提交于 2020-12-01 12:14:10

问题


I want to create 'games' which each have their own unique access 'code'. The code is required in the schema, and I need to generate a code each time a new game is created.

I thought schema.pre('init') would be a good place to generate this access code:

GameSchema.pre('init', function(next) {
    // Code generation logic happens here
    this.code = myNewlyGeneratedCode
    next()
}

Unfortunately, this returns an error message: ValidationError: Game validation failed: code: Path 'code' is required.

Why doesn't this work? Do I have to just create a code before I instantiate a new game?


回答1:


As mentioned in the comments, pre('save') is the middleware that runs before your document gets stored in the db. pre('init') gets called on your documents when they are returned from mongodb queries.

The easiest way to demonstrate the order of document middleware is with a simple example:

49768723.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const Schema = mongoose.Schema;

var count = 0;

const schema = new Schema({
  name: String
});

function log(str) {
  console.log(`${++count}: ${str}`);
}

schema.pre('save', function () {
  log('pre-save');
});

schema.pre('init', function () {
  log('pre-init');
});

schema.post('save', function () {
  log('post-save');
});

schema.post('init', function () {
  log('post-init');
});

schema.pre('validate', function () {
  log('pre-validate');
});

schema.post('validate', function () {
  log('post-validate');
});

schema.pre('remove', function () {
  log('pre-remove');
});

schema.post('remove', function () {
  log('post-remove');
});


const Test = mongoose.model('test', schema);

const test = new Test({ name: 'Billy' });

async function main() {
  await test.save();
  log('saved');
  await Test.findOne({ _id: test.id });
  log('found');
  await test.remove();
  log('removed');
  return mongoose.connection.close();
}

main();

output

stack: ./49768723.js
1: pre-validate
2: post-validate
3: pre-save
4: post-save
5: saved
6: pre-init
7: post-init
8: found
9: pre-remove
10: post-remove
11: removed
stack:


来源:https://stackoverflow.com/questions/49768723/when-exactly-does-mongooses-preinit-get-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!