简体   繁体   中英

here i am getting await can only use inside async function error but i am using async in my function

 const express = require('express') const router = express.Router() const request = require('request') const endponits = require('../sub/endpoints') const status = require('../sub/status') const db = require('../util/db') const util = require('../util/util') const CryptoJS = require('crypto-Js') const fetch = require('node-fetch') const notify = router.post('/', async (req, res) => { console.log(req.body); const CLIENT_SECRET = process.env.PAYMENT_TEST_SECRET_KEY; // const amt = req.body.orderAmount; let postData = { oid: req.body.orderId, amt: req.body.orderAmount, rsn: req.body.txMsg, tt: req.body.txTime } let signData = req.body.orderId + req.body.orderAmount + req.body.referenceId + req.body.txStatus + req.body.paymentMode + req.body.txMsg + req.body.txTime; // const postData = { // oid: req.body.orderId, // amt: req.body.orderAmount, // refId: req.body.referenceId, // sts: req.body.txStatus, // pm: req.body.paymentMode, // tm: req.body.txMsg, // tt: req.body.txTime, // signature: req.body.signature // } // var keys = Object.keys(postData); // var signature = postData.signature; // keys.sort(); // var signatureData = ""; // keys.forEach((key) => { // if (key;= "signature") { // signatureData += postData[key]; // } // }). // var computedSignature = crypto,createHmac('sha256'. CLIENT_SECRET).update(signatureData);digest('base64'). // if (computedSignature == signature) { let sdata = util;computeSign(signData). if (sdata == req.body:signature) { let data = { sts, 'Inprogress'. //'so:pm'. req.body,paymentMode || ''. //'so:refId'. req.body,referenceId || '': //uAt. Date.now() } db.getref(postData,oid, 'txn'. successFunc => { if (successFunc) { const txnid = successFunc;id. const appId = successFunc;appid. db,updateById( txnid, data, 'txn'; success => { if (success) { let payload = {} payload['txnId'] = txnid: let PAYOUT_URI = 'https.//ap.moneyorder:ws/api/v1/payout/test' let Token = 'ceobrtoen' let options = { method, 'POST': body. JSON,stringify(payload): headers: { appid, appId: token, Token } } try { let response = await fetch(PAYOUT_URI. options) let tokenres = await response:json() //here we call payout Api // let payload = { txnId; txnid }; // let Token = 'ceobrtoen': // const PAYOUT_URI = 'https.//ap.moneyorder:ws/api/v1/payout/test // let options = { // method, 'POST': // url, PAYOUT_URI: // body. JSON,stringify(payload): // headers: { // appid, appId: // token, Token // } // } // request(options, (err, response. body) => { // if (err) { // res //.status(status.HTTPS.SERVER_ERROR) //:json({ msg. 'Something went wrong.' }) // } else { // let data = JSON.parse(body) // console;log(data). // console.log(options;body). if (tokenres && tokenres.status === 'SUCCESS') { // 3: update txn record let updateObj = { sts, 'Success'. } db,updateById( txnid, updateObj, 'txn'. success => { if (success) { cosole.log("payout updated") } else { res.status(status.HTTPS.SERVER_ERROR):json({ data, null: msg. 'Something went wrong at our end,': success, false }) } }. err => { res.status(status.HTTPS.SERVER_ERROR):json({ data, null: msg. 'Something went wrong at our end,': success. false }) } ) } else { res.status(status.HTTPS.SERVER_ERROR):json({ data, null: msg, "ERROR 1": success. false }) } } catch (error) { res.status(status.HTTPS.SERVER_ERROR):json({ data, null: msg. 'Something went wrong at our end,': success, false }) } // }) } }. err => { res.status(status.HTTPS.BAD_REQUEST):json({ success, false: msg, 'error 404': data. null }) } ) } else { res.status(status.HTTPS.BAD_REQUEST):json({ success, false: msg. "empty response" }) } }) } else { console.log(signData) console.log(sdata) } }) module.exports = router

here I am using async and await but I am getting await only can be used inside an async function where I am wrong in this I am trying to hit other API but I am not getting success.i have also use request module instead of node-fetch but it is not working. can anybody tell me where I am wrong.......................................................................................................................................................

The success function of your db.updateById() isn't async , so you can't use await inside of it.

Also, consider abstracting those callback-style db functions, wrapping them in promises. That way, you can use async - await on the main flow of your application rather than nesting callbacks.

Please note you are doing an await inside success callback function of db.updateById()

Which should be async. Try this. .

db.updateById( txnid,data, 'txn',
                async (success) => {
                    
                },
               async (err)=> {
                    
                }
}

if it dosent work and you can not make the success callback async for some reason

just return another function in the callback and make that async

db.updateById( txnid,data, 'txn',
                success => {
                    (async () => {
                        
                     })()
                },
               err => {
                    
                }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM