简体   繁体   English

如何通过mongo .find调用在Node.js中执行此回调调用

[英]How does this callback call work in Node.js with mongo .find call

models/category.js 模型/category.js

var mongoose = require('mongoose');

// Category Schema
var categorySchema = mongoose.Schema({
  title: {
    type: String
  },
  description: {
    type: String
  },
  created_at: {
    type: Date,
    default: Date.now
  }
});

var Category = module.exports = mongoose.model('Category', categorySchema);

// Get Categories
module.exports.getCategories = function(callback, limit) {
  Category.find(callback).limit(limit).sort([['title', 'ascending']]);
}

routes/categories.js 路线/categories.js

var express = require('express');
var router = express.Router();

Category = require('../models/category.js');

router.get('/', function(req, res, next) {
  Category.getCategories(function(err, categories) {
    if (err) res.send(err);
    res.render('categories', 
      { 
        title: 'Categories',
        categories: categories
      });
  });
});

router.post('/add', function(req,res) {
  res.send('Form Submitted');
});

module.exports = router;

I got a few questions about this code 我对此代码有一些疑问

a) how does the callback mechanism work from routes/categories.js when we pass that callback function to models/category.js in Category.find(callback). a)当我们将该回调函数传递给Category.find(callback)中的models / category.js时,回调机制如何从route / categories.js工作。 That seems bizarre to me since we are doing a whole res.render which becomes part of Category.find() ? 对我来说,这很奇怪,因为我们正在做一个完整的res.render,它成为Category.find()的一部分?

b) Where is limit specified? b)在哪里指定极限?

c) Why isn't there var in front of Category = require('../models/category.js'); c)为什么在Category = require('../ models / category.js')前面没有var;

a) that is indeed what happens, and is good: res.render will not get called until the find() operation executes on the database and a result is sent back for the mongoose code to return to you. a)确实会发生,并且很好:在数据库上执行find()操作并且将结果发送回给猫鼬代码返回之前,不会调用res.render You want to run the callback function after you get the result for your query, and so calling res.render before would be much more bizarre. 您想获得查询结果之后运行回调函数,因此在之前调用res.render会更加奇怪。

b) in the documentation. b)在文档中。 http://mongoosejs.com/docs/api.html#model_Model.find yields a Query object, which may be synchronously (ie before the query is actually made to resolve at the database) further specified with where , limit , etc. http://mongoosejs.com/docs/api.html#model_Model.find产生一个Query对象,该对象可以是同步的 (即,在实际查询要在数据库上解析之前),进一步用wherelimit等指定。

c) because someone got lazy. c)因为有人偷懒。 In this example it doesn't actually make a difference because without var (or const or let in modern JS) a variable declaration is tacked onto the local context, which in your file is the routes/categories.js module context, and because Categories is declared at the top of that scope, var doesn't change where the variable ends up being bound. 在此示例中,它实际上并没有什么不同,因为没有var (或在现代JS中为constlet ),变量声明会附加到本地上下文中,该上下文在您的文件中是routes/categories.js模块上下文,并且因为Categories在该范围的顶部声明, var不会更改变量最终绑定的位置。 But it's lazy, and for good example code, that should have var in front of it. 但这是懒惰的,并且对于良好的示例代码, 应该在其前面加上var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM