Home » Javascript » applying function with passed in arguments as well as own supplied

applying function with passed in arguments as well as own supplied

Posted by: admin August 19, 2018 Leave a comment

Questions:

I’m not sure of the technical term of what I’m trying to do so it makes it hard for me to find information. So what I’m trying to do is to pass known future arguments to a function, while also needing to pass a particular argument.

E.g.

app.post("/example", function (req, res) { 
    admin.retrieveMany(req, res, "list") 
});

So what I require in the future is the req res parameters, but at the same time, I need to pass a particular argument, in this case "list".

Is there a way to do this without needing a wrapper function? so just something like admin.retrieveMany(...args, "list")?

Update:

This is how my admin is exported

module.exports.admin = {
    create,
    drop,
    retrieveMany
}

This is the create function summarised:

function create(req, res, which) {
    try {
        var { name } = mixin.getProxy(req.body, res)
    }
    catch (err) {
        console.log(err)
        return
    }
    // SQL stuff
}

Proxy code:

function getProxy(obj, res) {
    return new Proxy(obj, { //create new Proxy object
        get: function (obj, prop) { //define get trap
            if (prop in obj) {
                return obj[prop];
            }
            else {
                if (res) {
                    res.send({
                        error: true,
                        error_message: "could not retrieve prop: " + prop,
                        success: false
                    })
                }
                throw new Error("Missing required key: " + prop); //throw error if prop doesn't exists
            }
        }
    });
}
Answers: