2
只看示例代碼MongoDB的驅動程序: http://mongodb.github.io/node-mongodb-native/2.2/tutorials/projections/如果沒有錯誤,node.js回調函數需要null?
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);
});
}
Shouln't最後一行的回調(文檔)是回調(NULL,文檔)?
根據node.js回調符號它應該,但開發人員可以使用自己的風格。在這種風格中,回調根本不接受「錯誤」。 – alexmac