【发布时间】:2019-02-20 14:37:19
【问题描述】:
我试图在通过猫鼬访问mongodb 时隐式使用promise-then。
如果我有相同日期和用户的新数据,我应该删除旧数据并插入新数据。
A 部分工作正常,但我认为.then 的全部意义在于防止缩进,就像我们在回调方法中所做的那样。
另外,在这种情况下,我不知道是否需要为每个.then 提供.catch。
但是 B 部分不起作用,因为在 .then(function(createdData){}) 中返回的 createdData 引用了 DailyData.remove() 方法,并返回一个完全不同的对象。
答:
DailyData.remove({date: getCurrentDate(), owner: currUser})
.then(function(){
DailyData.create({date: getCurrentDate(), owner: currUser})
.then(function(createdData){
createdDataGlobal = createdData;
UpdatedInnerData.remove({date: getCurrentDate(), owner: currUser})
.then(function(){
insertNewInnerData();
})
.catch(handleError);
})
.catch(handleError);
})
.catch(handleError);
乙:
DailyData.remove({date: getCurrentDate(), owner: currUser})
.then(function(){
DailyData.create({date: getCurrentDate(), owner: currUser});
})
.then(function(createdData){
createdDataGlobal = createdData;
UpdatedInnerData.remove({date: getCurrentDate(), owner: currUser});
})
.then(function(createdData){
insertNewInnerData();
})
.catch(handleError);
【问题讨论】:
标签: node.js mongodb mongoose promise