原谅我的愚蠢,但我如何用承诺重写这些?
我读过mongoose支持承诺。不确定社交,但是console.log向我显示它返回对象,那一定是它吗?
mongoose.connect('mongodb://localhost/trends');
social.facebook(urlValue, function(err, signals) {
console.log(signals);
});
var articleData = Article({
publisher: urlValue,
url: urlValue
});
articleData.save(function(err) {
if (err) throw err;
console.log('Url saved successfully!');
});
答案 0 :(得分:2)
如果你使用mongoose v4.x,articleData.save()
会返回一个承诺(cf http://mongoosejs.com/docs/promises.html)。
您的代码将变为:
articleData.save()
.then(function(doc) {
console.log('Url saved successfully!');
})
.catch(function(err) {
// Here is where you should deal with the error
});