How to get the value in result.rows[0] as a string? - javascript

I am querying a Cassandra database using Node.js, and receiving result. I can access each element of the result with result.rows[0] etc.
However, I am currently trying to do data processing of the result in Javascript. I tried to convert result.rows[0] to String with result.rows[0].data.toString() but this returned error Cannot read property toString of undefined.
I know that I do indeed have data in my result.rows[0]. What is the problem in this situation, and how would I go about acquiring the String contained in result.rows[0]? I have also tried getting toString() of result yet that is returning [object Object].

Related

JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native)

I have created a way to store data the way I want in AsyncStorage. This includes:
Reading a variety of input (various arrays)
Storing the input in a local this.state.object that houses multiple arrays
Stringifying this.state.object
Storing the stringified object in a temp array
Assigning the array with all objects to another state, this.state.allObjects
Stringifying 'this.state.allObjects'
Storing stringified array in AsyncStorage
The end goal of all of this is to have an array of objects that each represent a very different instance of the same type (with different parameters etc.). It may not be the most elegant approach, but it seems to store and load fine.
The issue arises when I try to parse anything from this.state.allObjects[x]. Or rather, it only occurs when I need to access it in a case that is NOT right after I load it.
During my loadFromAsync function, I am perfectly able to execute this.state.allObjects = JSON.parse(fromAsync); to get the array of stringified objects and then var display = JSON.parse(this.state.allObjects[0]).name to retrieve the name of the first parsed object of the array.
Any time outside of this function when I try to call var display = JSON.parse(this.state.allObjects[0]).name or even simpler <Text>{JSON.parse(this.state.allObjects[0]).name}</Text> I receive the following errors:
when running
const parse = JSON.parse(this.state.allObjects[0]);
//JSON Parse error: Unexpected identifier "undefined"
console.log(parse.name);
and when running
const parse = JSON.parse(this.state.allObjects[0]).name;
//JSON Parse error: Unexpected identifier "undefined" + null is not an object (evaluating 'JSON.parse(this.state.allObjects[0]).name
console.log(parse);
and lastly while running
const parse = JSON.parse(this.state.allObjects);
//JSON Parse error: Unexpected EOF + JSON Parse error: Unexpected token ','
I assume that this.state.allObjects changes somewhere within my code, or appends an extra } somewhere, though it really shouldn't. When I display this.state.object and this.state.allObjects[0], their format is exactly the same visually. Adding more objects to this.state.allObjects and displaying each stringified component also works; it is just a matter of actually parsing these components that is not working outside the original load function.
I am extremely stuck. Any advice is appreciated. If I need to change my datatype, that's fine. It's just a bit annoying to have come this far with a stringified array of stringified objects holding arrays and not be able to parse it.
Because this.state.allObjects Already JSON object.
Try JSON.stringify()

How to get JavaScript Object from '[object Object]' string?

I'm working on a WebView app in React Native, and I need to handle the messages sent by website (using postMessage) using the onMessage callback. As per the docs -
window.postMessage accepts one argument, data, which will be available
on the event object, event.nativeEvent.data. data must be a string.
But the the string (event.nativeData.data) that I am getting in my onMessage() function is '[object Object]' which I know is the result of calling toString() method on an Object.
Is there any way to get back the JavaScript Object obj from the resulting String obtained using obj.toString() ?
I tried JSON.parse() but it only gives the error as shown in the attached image below.
Thanks !
Is there any way to get back the JavaScript Object obj from the resulting String obtained using obj.toString() ?
No. The data in the object simply does not appear in the string.
You can tell this just by looking at the string! There is nothing that either resembles the original data nor which looks like an encoded something.
If you want to preserve the data, then use JSON.stringify to convert it to a string instead of converting it with .toString().

What object has a String representation of `{name=value}` in JavaScript?

I receive a complex value in JavaScript (inside the JDK Nashorn engine), that I have to interact with. That value prints to the console as {shown=true}. When I say typeof value I receive object as an answer. When I say Object.keys(value); I receive a TypeError: {shown=true} is not an Object in .... Whey I say value.shown or value["shown"] I always receive a null.
What is type is this mysterious object, and how do I access the value of the "shown" property correctly?
Unfortunately, it is not easy to create a simple example and I cannot debug interactively... Any help is highly appreciated!
Edit:
JDK is JavaSE-1.8.
Calling JSON.parse(value); results in
javax.script.ScriptException: SyntaxError: Invalid JSON: <json>:1:1 Expected , or } but found s
{shown=true}
^
Assuming what you got is a Java object, you should be able to call value.getClass() to get its Java class.
From its string representation, it might be an instance of java.util.HashMap or similar. If so, you should be able to access the value of the "shown" property via value.get("shown").

Iterating PHP namespaced objects in ng-repeat

I'm trying to iterate trough array of objects using ng-repeat in Angular. Everything works fine except for an API, which returns json encoded PHP object, which resides in different namespace on server. That leads to situation, where returned object properties contains \ in name and I can't find the way how to access them while using {{Namespace\Property}} syntax in Angular. \ causes syntax error on client side, even when trying to escape this character doubling it.
Logging JSON-decoded object in browser returns correct contruction, but can't figure the way to read specified property.
Example output returned from API:
0: Object
Auth\Username: admin
Auth\Domain: acme.org
Auth\Enabled: true
1: Object
Auth\Username: guest
Auth\Domain: acme.org
Auth\Enabled: false

Cannot read property 'MyProperty' of undefined

In my Angular2 template, I have the following binding:
{{Stringify(result)}}
{{result.MyProperty}}
Stringify is a function that returns JSON.stringify of the input object. The Stringify function returned a JSON string that shows the name and value of MyProperty.
However, the second line returns a
TypeError. Cannot read property 'MyProperty' of undefined in {{result.MyProperty}}.
JSON.stringify clearly shows that this property / field exists, so why am I getting an error?
If it's there it can be accessed and JS wouldn't throw
Try instead
{{result?.MyProperty}}
maybe Angular makes an attempt to access result.MyProperty before result has a value while Stringify(result) doesn't choke on null.
When result is updated in the meantime (maybe because the value was received from the server, the view would update before you can recognized that an empty string was shown before.
Your question doesn't provide enough context to know.
See also
https://github.com/angular/angular/issues/791
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#expression-operators (from a comment below - thanks to #MarkRajcok )

Categories