Applying user selected texture to cube dynamically?

Here is what I am trying to do I’m creating a cube using BoxGeometry and trying to apply the texture that the user has selected.
I created a raycaster and use that raycaster inside mouse down event.
if the object is present on cursor position then a user-selected texture gets applied.
So far everything works fine then I added if-else condition if the user selected single image for texture then that single image gets applied whole body of cube means all side of cube gets that image as material.
but if the user selected more than one image suppose three images then only three sides of cube get material this also works very well now a real problem when user select only three images remaining side of cube remain black and that is okay only material applied side shows up.
now if user again select a single image and click on cube to apply single image texture to whole body again ray caster gives problem that
“–can’t access property “side”, the material is undefined–”

what should i do that raycaster works well with cube which dont have all side covered with material.