When working on setting a default value for the db
in my CRUD functions for testing purposes, I encountered a peculiar issue that has left me puzzled. Here's the snippet of code that caused the trouble:
import { db } from './firebase'
function getUsers({ db = db }) {
try {
return db
...
During tests, using this function posed no problems as I provided a test database to invoke it. However, the real application should be able to use the default value and call the function without any parameters. Surprisingly, I encountered the error message:
ReferenceError: can't access lexical declaration 'db' before initialization
. The reason behind this error remains unknown to me.
To resolve this issue, I resorted to renaming the parameter. Nonetheless, I am eager to gain insights into understanding what exactly transpired here. Does anyone have an idea?
Workaround Implemented:
import * as firebase from './firebase'
function getUsers({ db = firebase.db }) {
try {
return db