2

I am storing dateBirth as milliseconds in neo4j.

When using the browser I get this:

match (u:User) return u.dateBirth // Returns -2209078800000 

When using the bolt driver (neo4j v3-javascript) I get this:

match (u:User) return toInt(u.dateBirth) as dateBirth
// Returns: Integer { low: -1465609856, high: -515 }

How do I convert this Integer Object to a primitive number in JavaScript?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Jingle
  • 573
  • 3
  • 15
  • 1
    What happends if you use `toFloat` instead of `toInt`? – Wikiti Jun 26 '16 at 11:35
  • There is a documentation about integers in the driver's readme https://github.com/neo4j/neo4j-javascript-driver#write-integers – Christophe Willemsen Jun 26 '16 at 12:59
  • Possible duplicate of [Why does Number() return wrong values with very large integers?](http://stackoverflow.com/questions/35727608/why-does-number-return-wrong-values-with-very-large-integers) –  Jun 26 '16 at 15:08

2 Answers2

3

As stated in the readme:

The Neo4j type system includes 64-bit integer values. However, Javascript can only safely represent integers between -(2^53 - 1) and (2^53 - 1). In order to support the full Neo4j type system, the driver includes an explicit Integer types. Any time the driver receives an Integer value from Neo4j, it will be represented with the Integer type by the driver.

You can use toInt() (assuming a 32 bit integer) or toNumber() to the convert to the nearest floating point JavaScript Number type.

The detailed docs on the Integer type are available here.

ozanmuyes
  • 721
  • 12
  • 26
William Lyon
  • 8,371
  • 1
  • 17
  • 22
  • using toFloat() instead of toInt() worked. `match (u:User) return toFloat(u.dateBirth) as dateBirth` This link was very useful. Where did you find that? http://neo4j.com/docs/api/javascript-driver/current/class/src/v1/integer.js~Integer.html – Jingle Jun 28 '16 at 17:36
1

I'm using this function found in here. Works great so far!

function toNumber({ low, high }) {
  let res = high

  for (let i = 0; i < 32; i++) {
    res *= 2
  }

  return low + res
}
DurkoMatko
  • 5,238
  • 4
  • 26
  • 35