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: '指标不存在'
            });
        };
    })
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章