Emanuela Colta
Emanuela Colta

Reputation: 2207

Get firebase key after push ( python2.7 )

I am using Python 2.7 and Pyrebase(as a package to communicate with my Firebase database). I am trying to get the key() of the newly pushed object in my database. I did this a lot of times with node.js, but I need some help to do it with Python 2.7 Here is my API:

@APP.route('/api/product', methods = ['POST'])
def products():
    product_details = {
        "title": request.form['title'],
        "description": request.form['description']
    }
    new_product = DB.push("product").push(product_details)
    print new_product.get().key()

    return json.dumps(apiResponse.success(product_details))

The object will be inserted into the database, but I have no idea how to get its key. In node.js, I would use a promise and after insertion, I will get the key.

P.S: I cannot use .ref() since I use Pyrebase package

Upvotes: 1

Views: 678

Answers (1)

Emanuela Colta
Emanuela Colta

Reputation: 2207

I added this to my code: rec = DB.child("product").push(product_details) and this way, I was able to get the key.So, my code looks like this now:

@APP.route('/api/product', methods = ['POST'])
def products():
    product_details = {
        "title": request.form['title'],
        "description": request.form['description']
    }
  DB.child("product").push(product_details)
  rec = DB.child("product").push(product_details)   
  DB.child("users").child(owner_details['uid']).child('products').update({rec['name']: "true"})
  return json.dumps(apiResponse.success(product_details))

Upvotes: 2

Related Questions