0

I have a collection named Seller in my Firestore database, I've already done adding documents inside it. But my problem now is I want to add a Document with a SubCollection inside Seller named Category and Product, any Idea? here is my Add Product Activity code

class AddProductActivity : BaseActivity(), View.OnClickListener {
private lateinit var binding: ActivityAddProductBinding
private lateinit var mProductDetails: Product
private lateinit var mSellerDetails: Seller
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityAddProductBinding.inflate(layoutInflater)
    setContentView(binding.root)
    supportActionBar?.hide()
    mProductDetails = Product()
    mSellerDetails = Seller()

    binding.btnSubmitProduct.setOnClickListener(this)
}


fun loadProductsDetailsSuccess(product: Product) {
    mProductDetails = product
    hideProgresDialog()

    GlideLoader(this@AddProductActivity).loadUserPicture(
        product.productPict,
        binding.tvProductImageSetting
    )
    binding.etProductName.setText(product.productName)
    binding.etCategoryName.setText(product.category)
    binding.etProductPrice.setText(product.productPrice.toString())
    binding.etProductStock.setText(product.productStock.toString())

}


private fun validateProductDetails(): Boolean {
    return when {
        TextUtils.isEmpty(binding.etProductName.text.toString().trim { it <= ' ' }) -> {
            showErrorSnackBar(resources.getString(R.string.err_msg_enter_first_name), true)
            false
        }

        TextUtils.isEmpty(binding.etCategoryName.text.toString().trim { it <= ' ' }) -> {
            showErrorSnackBar(resources.getString(R.string.err_msg_enter_last_name), true)
            false
        }

        TextUtils.isEmpty(binding.etProductPrice.text.toString().trim { it <= ' ' }) -> {
            showErrorSnackBar(resources.getString(R.string.err_msg_enter_email), true)
            false
        }

        TextUtils.isEmpty(binding.etProductStock.text.toString().trim { it <= ' ' }) -> {
            showErrorSnackBar(resources.getString(R.string.err_msg_enter_password), true)
            false
        }
        else -> {
            showErrorSnackBar(resources.getString(R.string.please_wait), false)
            true
        }
    }
}


private fun addProduct(productInfo: Product, sellerInfo: Seller) {

    if (validateProductDetails()) {

        showProgressDialog(resources.getString(R.string.adding_product))


        val db = FirebaseFirestore.getInstance()
        val product: MutableMap<String, Any> = HashMap()

        db.collection(Constants.SELLER)
            .document(sellerInfo.id)
            .collection(Constants.CATEGORY)
            .document(productInfo.category)
            .collection(Constants.PRODUCT)
            .document(productInfo.id).set(product, SetOptions.merge())
            .addOnSuccessListener {
            hideProgresDialog()
            showErrorSnackBar(resources.getString(R.string.product_add_success), false)
            finish()
        }.addOnFailureListener {
            hideProgresDialog()
            showErrorSnackBar(resources.getString(R.string.product_add_failed), true)
        }

    }
}

fun addProductSuccess() {
    hideProgresDialog()

    Toast.makeText(
        this@AddProductActivity,
        resources.getString(R.string.msg_register_success),
        Toast.LENGTH_LONG
    ).show()
}

override fun onClick(v: View?) {
    if (v != null) {
        when (v.id) {
            R.id.btn_submit_product -> {
                addProduct(mProductDetails, mSellerDetails)
            }
        }
    }
}
}

and then i got this error message

    E/InputEventSender: Exception dispatching finished signal.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but seller has 1
        at com.google.firebase.firestore.DocumentReference.forPath(DocumentReference.java:81)
        at com.google.firebase.firestore.CollectionReference.document(CollectionReference.java:104)
        at com.adit.bangkit.plagroid.ui.activity.seller.product.AddProductActivity.addProduct(AddProductActivity.kt:90)
        at com.adit.bangkit.plagroid.ui.activity.seller.product.AddProductActivity.onClick(AddProductActivity.kt:121)
        at android.view.View.performClick(View.java:7350)
        at android.view.View.performClickInternal(View.java:7327)
        at android.view.View.onKeyUp(View.java:14924)
        at android.widget.TextView.onKeyUp(TextView.java:8616)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2866)
        at android.view.View.dispatchKeyEvent(View.java:14121)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:491)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1948)
        at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:472)
        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1850)
        at android.app.Activity.dispatchKeyEvent(Activity.java:4082)
        at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:122)
        at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
        at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:140)
        at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:599)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3090)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:386)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5896)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5764)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5259)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5316)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5282)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5434)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5290)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5491)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5263)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5316)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5282)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5290)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5263)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5316)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5282)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5467)
        at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5625)
        at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:3062)
        at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2604)
        at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2595)
        at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFi

i've no idea how to solved this

Tri Aditya
  • 33
  • 1
  • 8
  • 2
    One of the values you're using to create a `DocumentReference` does not have a value. The easiest way to find which one that is, is to put a breakpoint on the `db.collection(Constants.SELLER)...` line in `addDocument`, run the code in the debugger, and check the value of each variable you're passing to Firestore. – Frank van Puffelen Mar 20 '22 at 14:06
  • Have you solved the issue? – Alex Mamo Mar 21 '22 at 09:35
  • Does this answer your question? [Firebase Cloud Firestore : Invalid collection reference. Collection references must have an odd number of segments](https://stackoverflow.com/questions/46639058/firebase-cloud-firestore-invalid-collection-reference-collection-references-m) – ashwath hegde Apr 01 '22 at 17:30

0 Answers0