I want to pass an object via NPM script like
"update-user-roles": "ts-node user-roles.ts {PAID_USER: true, VIP: true}"
My function picks up the object but keeps adding additional commas so it's not updating the user correctly. How do I receive the object as is?
async function updateUserRoles(roles: any) {
const userID = await getAuth().then((res) => res.uid);
updateUser({
userID: userID,
fields: {
roles: {
roles
},
}
})
console.log(`User roles successfully added: ${roles}`)
}
const rolesString = JSON.stringify(process.argv.slice(2))
updateUserRoles(JSON.parse(rolesString))
I get the following message:
User roles successfully added: {PAID_USER:,true,,VIP:,true}
I couldn't get any of these answers to work, finally got a solution using the following:
The script:
"ts-node user-roles.ts '{\"PAID_USER\": true, \"VIP\": true}'"
Parse the object string back into an object using JSON.parse and pass to function:
const objArgStr = process.argv[2];
if (!objArgStr) {
console.error('No argument provided.');
process.exit(1);
}
const objArg = JSON.parse(objArgStr);
console.log('Object argument: ', objArg);
updateUserRoles(objArg)