41298

Error: Firebase.update failed: First argument contains NaN in property

Question:

I am making a web app that allows the user to grow a tree. When the user fertilizes the tree, the attribute "fertilizer" should increment. However, the app keeps giving me the error because it has decided that "fertilizer" is undefined.

The update score function looks like:

function updateScore(){ if (usr!=null){ var tree = $firebase(new Firebase("https://myapp.firebaseio.com/trees/"+usr.treeid)); var usrTree = tree.$asObject(); var data = null; if (typeof usrTree.data !== 'undefined'){ data = usrTree.data; } console.log(usrTree); console.log(usrTree.$id); console.log(usrTree.uid); console.log(usrTree.fertilizer); tree.$update({fertilizer: usrTree.fertilizer+1}); } } }

So the console gives these results:

e {$$conf: Object, $id: "-JcDm0UlYVGFfFAX0GSf", $priority: null, $save: function, $remove: function…} $$conf: Object $id: "-JcDm0UlYVGFfFAX0GSf" $priority: null fertilizer: 0 level: 0 pesticide: 0 sunshine: 0 uid: "simplelogin:6" water: 0 __proto__: Object -JcDm0UlYVGFfFAX0GSf undefined undefined

I believe the first one is the usrTree object, the second one is the $id and the third and the fourth should be uid and fertilizer but are caught undefined. However, in the usrTree object, uidand fertilizer do show up as defined and they exist in firebase forge:

<img alt="" class="b-lazy" data-src="https://i.stack.imgur.com/GFy6l.png" data-original="https://i.stack.imgur.com/GFy6l.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

What am I doing wrong here?? What should I do to update the data??

Answer1:

It turns out that I need to use $loaded. I changed the code to the following and it works now.

var tree = $firebase(FirebaseRef.child("trees").child(usr.treeid)); var usrTree = tree.$asObject(); usrTree.$loaded().then(function() { tree.$update({fertilizer: usrTree.fertilizer + 1}); }

Recommend

  • possible spotify portrait metadata caching bug
  • Retrieve date from Firebase, angularJS
  • Android firing intent from thread
  • How is firebase realtime database storage calculated
  • How is firebase able to detect “app_remove”
  • Using firebase with WatchKit
  • ActionBar with appcompat library v7 (ava.lang.IllegalStateException: You need to use a Theme.AppComp
  • angularjs - ng-show doesn't update class when $interval triggers
  • Is there a way to use Firebase Legacy console from new account
  • docker-compose: connection refused between containers, but service accessible from host
  • Cannot update firebase version [duplicate]
  • Firebase firestore cloud functions showing trigger type 'unknown'
  • Firebase, only get new children
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • Write output of for loop to multiple files
  • Android Database Error - getWriteableDatabase
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • cygwin cannot exec 'git-add--interactive' permission denied
  • error importing numpy
  • RectangularRangeIndicator format like triangular using dojo
  • Is there a amazon webstore API for customers?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Cross-Platform Protobuf Serialization
  • How to extract text from Word files using C#?
  • vba code to select only visible cells in specific column except heading
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Revoking OAuth Access Token Results in 404 Not Found
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app