0

Hello I am creating 1 function with dynamic arguments where as I am calling api and on defined route I am calling express middleware function and from there I am calling another dynamic function which will help me to insert data into the database.

I am using Sequalize ORM

Here is code:

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
    })
}

In module.export I have added insertLog function which is getting called in api and from there I am calling insertLogFn() which is declared outside of the module.export.

I am able to get inserted result in function insertLogFn() but the things is await is not working and not waiting for the result.

What I want to do is to wait till insertLogFn gets executed and the returned response has to be stored in the variable and return it as an api response.

Mysterious Coder
  • 148
  • 2
  • 4
  • 20

1 Answers1

0

You cannot. As per my understanding, IMO, Thumb rule is "Async/Await operation should return a promise"

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
})
}

Now the answer will be clear if you read this one from Bergi: https://stackoverflow.com/a/40499150/9122159