anync庫+mongoose批量操作數據

錯誤的批量操作:

const pi_library_del = function (req, res) {
    const i18n = req.i18n;
    const pi_id = req.body.pi_id; // pi_id是一個全是id的數組
    
    PerformanceIndicatorClient.find({
        _id :{
            $in: pi_id
        }
    }).exec(function(err,pi){
        async.times(pi && pi.length,function(n,next){
            if(err){
                return res.json({
                    code:'ERR',
                    msg:'內部服務器錯誤:' + err
                })
            }
            if(pi[n].ref_num){
                return res.json({
                    code :'ERR',
                    msg: sprintf('指標<strong>%s</strong> 已經被使用,不能刪除!', pi[n].pi_name),
                });
            }else{
                pi[n].terminated = true;
                pi[n].is_sync = false;
                pi[n].save(function (err, pi) {
                if (err) {
                    return res.json({
                        code: 'ERR',
                        msg: msg,
                    });
                }
                return res.json({
                    code: 'OK',
                    msg: sprintf('指標 <strong>%s</strong> 刪除成功!', pi[n].pi_name),
                    });
                })
            }
        },next)
    },function(err,result){
        if (err) {
			res.json({
				code: 'ERR',
				msg: '刪除成功!'
			})
		} else {
			res.json({
				code: 'OK',
				msg: '刪除失敗!'
			})
		}
    }
    )
}

正確的 批量操作

const pi_library_del = function(req, res) {
        const i18n = req.i18n;
        const  pi_id = req.body.pi_id;   // pi_id 是一個全是ID的數組
  
        PerformanceIndicatorClient.find({
            _id: {
                $in: pi_id
            }
        }).exec(function(err, pis) {
            if (err) {
                console.log(err)
                return res.json({
                    code: 'ERR',
                    msg: '內部服務器錯誤:' + err
                })
            } else {
                async.times(pis.length, function(n, next) {
                    const pi = pis[n];
    
                    if (pi.ref_num) {
                        next(null, sprintf('指標<strong>%s</strong> 已經被使用,不能刪除!', pi.pi_name));
                    } else {
                        pi.terminated = true;
                        pi.is_sync = false;
                        pi.save(next)
                    }
                }, function(err, result) {
                    if (err) {
                        res.json({
                            code: 'ERR',
                            msg: i18n.t("app:common.save_fail"),  // '保存失敗!'
                        })
                    } else {
                        res.json({
                            code: 'OK',
                            msg: i18n.t("app:common.save_success"),  // '保存成功!'
                        })
                    }
                })
            }
        })
    }

正確的刪除單個數據:

const pi_library_del = function (req, res) {
    const i18n = req.i18n;
    const pi_id = req.body.pi_id;

    PerformanceIndicatorClient.findById(pi_id, function (err, pi) {
        if (err) {
            return res.json({
                code: 'ERR',
                msg: '內部服務器錯誤:' + err
            });
        };
        if (pi) {
            if (pi.ref_num) {
                return res.json({
                    code: 'ERR',
                    msg: sprintf('指標 <strong>%s</strong> 已經被使用,不能刪除!', pi.pi_name),
                });
            } else {
                pi.terminated = true;
                pi.is_sync = false;
                pi.save(function (err, pi) {
                    if (err) {
                        return res.json({
                            code: 'ERR',
                            msg: msg,
                        });
                    }
                    return res.json({
                        code: 'OK',
                        msg: sprintf('指標 <strong>%s</strong> 刪除成功!', pi.pi_name),
                    });
                })
            }
        } else {
            return res.json({
                code: 'ERR',
                msg: '指標不存在'
            });
        };
    })
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章