0

我不知道用 next 和没有 next 调用中间件有什么区别

没有下一个的例子所有中间件都运行得很好没有下一个也

tourSchema.pre('save', function () {
  console.log('second middleware is getting called');
  const currentDoc = this;
  const slugDocument = slugify(currentDoc.name, { lower: true });
  this.slug = slugDocument;
  console.log('after addition of slug', this);
});
tourSchema.pre('save', function () {
  console.log('third pre middleware is called');
});
tourSchema.pre('save', function () {
  console.log('4th middleware is getting called');
});

使用 next 似乎与调用 next 或没有 next 没有区别。那么这两者之间的主要区别是什么。

tourSchema.pre('save', function (next) {
  console.log('second middleware is getting called');
  const currentDoc = this;
  const slugDocument = slugify(currentDoc.name, { lower: true });
  this.slug = slugDocument;
  console.log('after addition of slug', this);
  next();
});
tourSchema.pre('save', function (next) {
  console.log('third pre middleware is called');
  next();
});
tourSchema.pre('save', function (next) {
  console.log('4th middleware is getting called');
  next();
});
4

0 回答 0