[英]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.在这里我使用异步和等待,但我得到的等待只能在异步 function 中使用,我在这方面错了我试图打其他 API 但我没有成功。我也使用请求模块而不是节点-获取,但它不工作。 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.您的db.updateById()
的success
function 不是async
,因此您不能在其中使用await
。
Also, consider abstracting those callback-style db
functions, wrapping them in promises.另外,考虑抽象那些回调风格的db
函数,将它们包装在 Promise 中。 That way, you can use async
- await
on the main flow of your application rather than nesting callbacks.这样,您可以在应用程序的主要流程上使用async
- await
而不是嵌套回调。
Please note you are doing an await inside success callback function of db.updateById()请注意,您正在等待 db.updateById() 的成功回调 function
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只需在回调中返回另一个 function 并使其异步
db.updateById( txnid,data, 'txn',
success => {
(async () => {
})()
},
err => {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.