Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Async/Await:调用的函数不是正在写入和返回值_Javascript_Node.js_Ecmascript 6_Async Await_Es6 Promise - Fatal编程技术网

Javascript Async/Await:调用的函数不是正在写入和返回值

Javascript Async/Await:调用的函数不是正在写入和返回值,javascript,node.js,ecmascript-6,async-await,es6-promise,Javascript,Node.js,Ecmascript 6,Async Await,Es6 Promise,您好,我正在创建一个带有动态参数的函数,当我调用api时,在定义的路径上,我调用express middleware函数,然后从那里调用另一个动态函数,它将帮助我将数据插入数据库 我正在使用Sequalize ORM 以下是代码: var async = require('async'); // Models var LogSchema = require('../models/Logs') module.exports = { insertLog: async (req, res

您好,我正在创建一个带有动态参数的函数,当我调用api时,在定义的路径上,我调用express middleware函数,然后从那里调用另一个动态函数,它将帮助我将数据插入数据库

我正在使用Sequalize ORM

以下是代码:

var async = require('async');

// Models
var LogSchema = require('../models/Logs')

module.exports = {

    insertLog: async (req, res) => {

        let result = await insertLogFn('1', 'method_name()', 'module_name_here', 'req.body', '{ api response }', 'action', '24')
        console.log("result", result)
        res.status(200).json(result)
    }
};

function insertLogFn(status, invokedMethodName, moduleName, bodyRequest, apiResponse = null, actionName = null, userId) {
    async.waterfall([
        (nextCall) => {
            let dataToBeInserted = {}
            dataToBeInserted.status = status,
            dataToBeInserted.invoked_method_name = invokedMethodName,
            dataToBeInserted.module_name = moduleName,
            dataToBeInserted.body_request = bodyRequest,
            dataToBeInserted.api_response = apiResponse
            dataToBeInserted.action_name = actionName,
            dataToBeInserted.user_id = userId

            LogSchema.create(dataToBeInserted).then(res => {
                const dataObj = res.get({plain:true})
                nextCall(null, {
                    status: 200,
                    message: "Log inserted successfully",
                    data: dataObj
                })
            }).catch(err => {
            })
        }
    ], (err, response) => {
        if(err) {   
        }
        return response
    })
}
module.export
中,我添加了
insertLog
函数,该函数在api中被调用,从那里我调用
insertLogFn()
,该函数在
模块外部声明。export

我能够在函数
insertLogFn()
中获得插入的结果,但等待的东西不起作用,也不等待结果


我要做的是等待执行
insertLogFn
,返回的响应必须存储在变量中并作为api响应返回。

您不能。根据我的理解,IMO的经验法则是“异步/等待操作应该返回一个承诺”

函数insertLogFn(状态、invokedMethodName、moduleName、bodyRequest、apiResponse=null、actionName=null、userId){
异步瀑布([
(nextCall)=>{
让dataToBeInserted={}
dataToBeInserted.status=状态,
dataToBeInserted.invoked\u方法\u名称=invokedMethodName,
dataToBeInserted.module_name=moduleName,
dataToBeInserted.body\u request=bodyRequest,
dataToBeInserted.api_response=apiResponse
dataToBeInserted.action\u name=actionName,
dataToBeInserted.user\u id=userId
创建(dataToBeInserted)。然后(res=>{
const dataObj=res.get({plain:true})
nextCall(空{
现状:200,
消息:“日志插入成功”,
数据:dataObj
})
返回;

console.log("您应该在此处返回一些内容,因为
insertLogFn
不返回承诺。
async/await
async
模块是两个不同的东西。您不能同时使用它们。@NikKyriakides我在其他方面都使用过这两个模块module@AZ_我认为async/await是promise的替代方案。如果我错了,请纠正我。@AZ_Ohh我明白了我在使用异步/等待与ORM方法,这是返回承诺。感谢澄清。+1
function insertLogFn(status, invokedMethodName, moduleName, bodyRequest, apiResponse = null, actionName = null, userId) {
async.waterfall([
    (nextCall) => {
        let dataToBeInserted = {}
        dataToBeInserted.status = status,
        dataToBeInserted.invoked_method_name = invokedMethodName,
        dataToBeInserted.module_name = moduleName,
        dataToBeInserted.body_request = bodyRequest,
        dataToBeInserted.api_response = apiResponse
        dataToBeInserted.action_name = actionName,
        dataToBeInserted.user_id = userId

        LogSchema.create(dataToBeInserted).then(res => {
            const dataObj = res.get({plain:true})
            nextCall(null, {
                status: 200,
                message: "Log inserted successfully",
                data: dataObj
            })
return ;
            console.log("you should return something  here<-------");
        }).catch(err => {
        })
    }
], (err, response) => {
    if(err) {   
    }
    return response
})
}