如果没有错误,node.jscallback函数需要null?

只要看看来自mongodb驱动程序的示例代码: http : //mongodb.github.io/node-mongodb-native/2.2/tutorials/projections/

var MongoClient = require('mongodb').MongoClient , assert = require('assert'); // Connection URL var url = 'mongodb://localhost:27017/test'; // Use connect method to connect to the server MongoClient.connect(url, function(err, db) { assert.equal(null, err); console.log("Connected correctly to server"); findDocuments(db, function() { db.close(); }); }); var findDocuments = function(db, callback) { // Get the documents collection var collection = db.collection( 'restaurants' ); // Find some documents collection.find({ 'cuisine' : 'Brazilian' }, { 'name' : 1, 'cuisine' : 1 }).toArray(function(err, docs) { assert.equal(err, null); console.log("Found the following records"); console.log(docs) callback(docs); }); } 

最后一行callback(文档)是callback(null,文档)?

这取决于你的callback。

有错误优先的callback函数,第一个参数是错误的,第二个参数是数据,如: callback (err, data)

但是,在Mongo的官方示例网页(您指出的那个网页)中,它们传递了一个没有错误参数的callback。 错误优先callback在Node的内置模块中无处不在,但是Node并不强制您使用它们。 在这个例子中,Mongo开发者决定这么做。

不过,您可以轻松地重写Mongo示例以使用错误优先callback。