简体   繁体   English

在这里我正在等待只能在异步 function 错误中使用,但我在 function 中使用异步

[英]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.

相关问题 我可以在异步 function 中使用 await async 吗? - Can I use await async inside an async function? 异步/等待过度杀伤? 我是否不必要地使用异步并等待? - Async/Await overkill? Am I unnecessarily using async and await? 当测试的 function 没有使用 async/await 时,我可以在 Jest 中使用 async/await 吗? - Can I use async/await in Jest, when the tested function is not using async/await? Javascript `await` “SyntaxError: Unexpted identifier” 即使我在一个 `async` 中等待 function - Javascript `await` “SyntaxError: Unexpted identifier” even if I am awaiting inside an `async` function 我在这里是否缺少有关异步/等待的信息? - Am I missing something here regarding async/await? 我的同步 function 在哪里? 尝试调用“等待”,但我收到错误“等待仅在异步 function 中有效。” - Where is my synchronous function? Trying to call "await" but I get the error "await is only valid in async function." 在等待时出错,该等待仅在使用异步函数时对异步函数有效 - Getting error on await, that await is valid only on async function when using async function SyntaxError: await 仅在异步中有效 function 我如何使用异步? - SyntaxError: await is only valid in async function how can i use async? 代码显示错误 await 只能在异步 function 中声明,尽管存在异步 function - code shows error await can only be declared inside async function although there is async function 我怎么能对那个 function 使用异步/等待? - how could I use async/await for that function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM