2015-09-10 11 views
8

Nodejs sunucumu Bluebird kitaplığı ile sözler kullanarak yeniden çalışmaya çalışıyorum, ancak basit bir sorunla karşılaşıyorum. çalışmıyorPromise.promisify ile "bu" doğru nasıl sağlanır?

Kötü Yol (çalışma ...)

adapter.getUsers(function(users){ 
    users.rows.forEach(function(item){ 
     user = item.username; 
     adapter.getNotifications(user, function(notificationList){ 
      console.log(notificationList); 
     }) 
    }); 
}); 

Şık Geçici Yolu (:

benim db kullanıcıları almak için sonra bu kullanıcıyla ilişkili tüm bildirim sınıfı listelemek istediğiniz Bu kodu çalıştırdığınızda ...)

var getNotifications = Promise.promisify(adapter.getNotifications); 
adapter.getUsers().then(function(users) { 
    users.rows.forEach(function(item){ 
     var dbUser = "sigalei/" + item.value.name; 
     console.log(dbUser); 
     return getNotifications(dbUser); 
    }); 
}).then(function(result){ 
    console.log(result); 
    console.log("NOTIFICATIONLIST"); 
}); 

Ancak benim getNotification yönteminin içine bu hatayı alıyorum:

Unhandled rejection TypeError: Cannot read property 'nano' of undefined at Adapter.getNotifications (/Users/DaniloOliveira/Workspace/sigalei-api/api/tools/couchdb-adapter.js:387:30) at tryCatcher (/Users/DaniloOliveira/Workspace/sigalei-api/node_modules/bluebird/js/main/util.js:26:23)

DÜZENLEME

user2864740`s değerli yorumların sonra hata bazı kapsam sorunu alakalı olduğunu tespit ettik. Yani, neden yöntemini kullanarak sonra, yöntemNotifications "bu" env değişkeni tanımak değil mi?

var Adapter = module.exports = function(config) { 
    this.nano = require('nano')({ 
     url: url, 
     request_defaults: config.request_defaults 
    }); 
}; 

Adapter.prototype.getNotifications = function(userDb, done) { 

    var that = this; 
    console.log(that); 
    var userDbInstance = that.nano.use(userDb); 
    userDbInstance.view('_notificacao', 'lista', 
     {start_key: "[false]", end_key: "[false,{}]"}, 
     function(err, body) { 
     if(err){ done(err); } 
     done(body); 
    }); 

}; 
+0

Hey, hata "promisify" olan getNotifications yönteminin içinde oluşturulur. –

+0

@ user2864740 Yine soruyu düzenledim, problemin kapsam değişkenleriyle ilgili olduğunu düşünüyorum ... –

+0

'.call() '' '' '' '' '' '' '' '' '' '' 'ile' '' 'yi deneyin. – Amit

cevap

6

yapmak değilken undefined Bu sadece problem of calling "unbound" methods çok yaygındır.

var getNotifications = Promise.promisify(adapter.getNotifications, {context: adapter}); 

Alternatif olarak, .bind() yöntemini gerekirdi ya adapter (kullanarak .call()) günü yeni getNotifications işlevini çağırır:
Bunu bağlı olması Promise.promisify için bir seçenek olarak bağlam geçebilir. Ayrıca Promise.promisifyAll(adapater) kullanmayı ve ardından adapter.getNotificationsAsync(…) numaralı telefonu kullanmayı düşünebilirsiniz.

Bunun hala işe yaramadığına dikkat edin. Bir döngü içinde basitçe sözler oluşturamazsınız - açıkça beklemeniz ve then geri aramadan bir söz vermeniz gerekir, aksi takdirde sadece döndürdüğünüz undefined değeri bir sonraki geri dönüşüme geçirilir.

adapter.getUsers().then(function(users) { 
    return Promise.all(users.rows.map(function(item){ 
     var dbUser = "sigalei/" + item.value.name; 
     console.log(dbUser); 
     return getNotifications(dbUser); 
    })); 
}).then(function(results) { 
    for (var i=0; i<results.length; i++) 
     console.log("result:", results[i]); 
}); 

yerine Promise.all(users.rows.map(…))

, Bluebird içinde ayrıca Promise.map(users.rows, …) kullanabilirsiniz.

+0

Doğru, bu soru için detaylı bir açıklama ve "ilişkisiz" yöntemler için teşekkür ederim. –

+1

Bluebird 3.4.6 bağlamında şu şekilde bağlanır: 'Promise.promisify (bağdaştırıcı.getNotifications, {context: adapter}); –

+0

@CryptixMaster Teşekkürler, 3.0 docs ve code :-) güncellendi – Bergi

1

Ne

basitçe yaklaşık
var getNotifications = Promise.promisify(adapter.getNotifications.bind(adapter)); 

veya muhtemelen

var getNotifications = Promise.promisify(function() { 
    return adapter.getNotifications.apply(adapter, arguments); 
}); 

?

ben de senin Sorunu anlamak emin değilim, ama bu this emin bağlıdır yapmalıdır ve return getNotifications(dbUser);

İlgili konular