2016-12-18 104 views
7

Gulp Veritabanımı, Gulp Data kullanırken, veri kaynağı olarak Mongo veritabanından nasıl alabilirim?Gulp Veriyi Kullanma Gulp Data kullanarak Gulp Veri

Gulp Görev ( basitleştirilmiş)

Ben prototip veritabanını kullanıyorum
gulp.task('db-test', function() { 
    return gulp.src('./examples/test3.html') 
     .pipe(data(function(file, cb) { 
      MongoClient.connect('mongodb://127.0.0.1:27017/prototype', function(err, db) { 
       if(err) return cb(err); 
       cb(undefined, db.collection('heroes').findOne()); // <--This doesn't work. 
      }); 
     })) 
     //.pipe(data({"title":"this works"})) -> This does work 
     .pipe(through.obj(function(file,enc,cb){console.log('file.data:'+JSON.stringify(file.data,null,2))})); 
    }); 

, ben çalıştırabilir,

> db.heroes.findOne() 

Ve bu sonuç almak:

{ 
    "_id" : ObjectId("581f9a71a829f911264ecba4"), 
    "title" : "This is the best product!" 
} 

cevap

5

Satırı değiştirebilirsiniz cb(undefined, db.collection('heroes').findOne()); aşağıda birini, aşağıda kısa el olarak

db.collection('heroes').findOne(function(err, item) { 
    cb(undefined, item); 
}); 

YA,

db.collection('heroes').findOne(cb); 

Yani senin Gulp görev Yukarıdaki basit olur sevmeye

gulp.task('db-test', function() { 
    return gulp.src('./examples/test3.html') 
     .pipe(data(function(file, cb) { 
      MongoClient.connect('mongodb://127.0.0.1:27017/prototype', function(err, db) { 
       if(err) return cb(err); 
       db.collection('heroes').findOne(cb); 
      }); 
     })) 
     //.pipe(data({"title":"this works"})) -> This does work 
     .pipe(through.obj(function(file,enc,cb){console.log('file.data:'+JSON.stringify(file.data,null,2))})); 
    });