通用模块在路由内的代码之前执行?

我已经在nodejs中创建了一个通用模块,并且我在要向所有用户发送通知的route中使用该通用模块,现在的问题是当我调用路由1时,执行了通用模块,然后route中的代码被执行,但我要执行的是第一条路由代码,然后执行通用模块。

这是路线,noti是通用模块

router.post('/',session,noti,async (req,res) => {
const dt = dateTime.create();
const formatted1 = dt.format('Y-m-d H:M:S');
try {
    const str = req.body.providerId;
    const data = str.split("|");
    const id = data[0];
    const name = data[1];
    const date = req.body.resultDate;
    const digit = req.body.winningDigit;
    const exist = await ABgameResult.findOne({ providerId: id,resultDate:  date,session: req.body.session });
    if (!exist) {
        const details = new ABgameResult({
            providerId: id,providerName: name,resultDate: date,winningDigit: digit,status: 0
        });
        const savedGames = await details.save();
        const update1 = await ABgamesProvider.updateone(
            {_id : id },{ $set: { providerResult: digit,modifiedAt: formatted1,resultStatus : 1 } }); 
            //resultStatus : 1 i.e; result is declared

        const Resultid = savedGames._id;
        const listData = {
            providerId : id,resultDate : date,status: 0,resultId: Resultid,providerName: name
        }

        return res.json({
            status : 1,message : "Success",data: listData
        });
    }
    else {
        const data = 'Details Already Filled For : '+ name +',Session : ' + req.body.session + ',Date: ' + req.body.resultDate;
        res.json(data);
    }
}catch (e) {
    console.log(e);
    res.json(e);
  }
});
  

现在我想要的是何时执行路由中的其他部分,而不是执行noti模块,我只想在部分代码中执行它

这是noti的模块

module.exports = async function ( req,res,next) {
try
{
    const token = await user.find({ banned : 1,andarBaharNotification: true },{firebaseId : 1});
        let userToken = [];
        for(index in token){
            userToken.push(token[index].firebaseId);
        }

        const digit = req.body.winningDigit;
        const str = req.body.providerId;
        const data = str.split("|");
        const name = data[1];
        var message = new gcm.Message({
                priority: 'high',data: {
                    title: " ("+name+")",icon: "ic_launcher",body: digit
                }
            });
            sender.send(message,{ registrationTokens: userToken },function (err,response) {
                if (err) console.error('error => ',err);
                else console.log('response=> ',response);
            });
        next()
    }
catch (e) {
    console.log(e);
    res.status(400).json({
        status: 0,message: 'Something Happpend',error: e
    });
  }
};
erfaya 回答:通用模块在路由内的代码之前执行?

我认为您想这样做!

const noti = require('./noti'); // importing noti
router.post('/',session,async (req,res) => {
   // DO YOUR STUFF
   noti(req,res);
});

从noti模块中删除next()

本文链接:https://www.f2er.com/3102740.html

大家都在问