I have a function which grabs some stuff from a database. I want to set the function to accept a DBConnection
string parameter and a ProductID
parameter, but because this will rarely be provided I would like a set a default value. I tried the following:
const DBConfig = require('./db.config');
async function getProduct(DBConnection = DBConfig.SQLConnection, ProductID) {
console.log(ProductID) // says undefined
await DBConnection.connect()
......
}
I call the above function from another file by doing this:
Product = await api.getProduct({ProductID: req.body.ProductID})
For some reason, doing the above does not work. That is, I always get undefined
in the console for ProductID
.
If I simply do the following, then I do get the correct ProductID
value printing in the console:
const DBConfig = require('./db.config');
const DBConnection = DBConfig.SQLConnection,
async function getProduct(ProductID) {
console.log(ProductID) // prints in the console as { ProductID: '1234' }
await DBConnection.connect()
......
}
I am using Webpack with babel-loader. I don't think it is a transpilation issue but not sure. Any ideas? In my Webpack config I have this to handle js
files:
{
test: /\.js$/,
include: [ srcPath ],
exclude: ['/node_modules/','/src/test'],
use: {
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env"] //Preset used for env setup
}
}
}
I think you want the parameters of getProduct
to be destructured:
function getProduct({DBConnection = DBConfig.SQLConnection, ProductID}) {
...
}
Then you can do:
Product = await api.getProduct({ProductID: req.body.ProductID})
Shouldn't be a webpack or babel issue