Skip to main content

Fire gives the "TypeError: Expected a message object, but got field_path" error when trying to use ArrayUnion

I am trying to use firebase with python. I have connected to the database and in the scheme as following:

import firebase_admin
from firebase_admin import credentials, db, firestore

# Initialize connection to firebase
cred = credentials.Certificate("serviceAccountKey.json")
app_options = {
    'databaseURL': '...'}
firebase_admin.initialize_app(cred)
db = firestore.client()

Currently the database has 2 types of documents, we get the following when we convert them to dictionaries:

Admin: {'users': [], 'email': '...', 'first_name': '...', 'last_name': '...'}
User: {'email': '...', 'onboarding_form': 'first_name': '...', 'last_name': '...'}

I want to add a reference of a user to the array of users which is a field of admin. For it I have the following code:

    user_id = get_user_id(user_last_name, user_first_name)
    admin_id = get_admin_id(admin_last_name, admin_first_name)

    user_ref = db.collection(u'user').document(user_id)
    admin_ref = db.collection(u'admin').document(admin_id)

    admin_ref.update(
        {u'users': firestore.ArrayUnion([user_ref])})

But the code does not insert the reference to array of users. I get the following error:

Traceback (most recent call last):
  File "/home/primrose/work/BE/db_api.py", line 109, in <module>
    test_run()
  File "/home/primrose/work/BE/db_api.py", line 106, in test_run
    add_user("...", "...", "...", "...")
  File "/home/primrose/work/BE/db_api.py", line 98, in add_user
    admin_ref.update(
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/document.py", line 324, in update
    batch, kwargs = self._prep_update(field_updates, option, retry, timeout)
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/base_document.py", line 239, in _prep_update
    batch.update(self, field_updates, option=option)
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/base_batch.py", line 141, in update
    write_pbs = _helpers.pbs_for_update(
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 941, in pbs_for_update
    update_pb.update_transforms.extend(field_transform_pbs)
TypeError: Expected a message object, but got field_path: "users"
append_missing_elements {
  values {
    reference_value: ".../databases/(default)/documents/user/hlCcG1oFW4Z75JKqp1ZcfM8xWlh1"
  }
}
.

I had to omit the names and the beginning of the reference_value therefore they are "...". I do not understand the error, what could have caused the error? Shouldn't providing a field path be right?

When I try to get the information of the admin I get the following:

    print(admin_ref.get().id, " => ", admin_ref.get().to_dict())
    {'users': [], 'email': '...', 'first_name': '...', 'last_name': '...'}

The used firebase_admin and protovuf version are:

Name: firebase-admin
Version: 5.2.0

Name: protobuf
Version: 4.21.2

The user_ref (former physio_ref before the edit, that name was not supposed to be in the question). the data I want to add to the array called "users" is a reference to another user.



source https://stackoverflow.com/questions/72946132/fire-gives-the-typeerror-expected-a-message-object-but-got-field-path-error

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype