Single Item Array Read as Object in React - javascript

I'm hoping someone can help me bug fix an issue I'm having where an array that contains a single item is read as an object in json data. I'm using .mapto loop over the values like defined here: https://facebook.github.io/react/docs/multiple-components.html
The build I'm using is: https://github.com/newtriks/generator-react-webpack, so I'm not sure if this is a webpack issue, fetch issue (how I'm requesting the data), or something much more obvious. Any insight would be appreciated. Thanks.

I found where the error was. I was using YQL as a proxy service to get around CORS. Turns out the default response is “lossy”, where single element arrays are returned as an object.. had me scratching my head for a while trying to figure out where this was coming from. Solution was to add jsonCompat=new to the query string. Listed in the docs here: https://developer.yahoo.com/yql/guide/response.html#json-to-json-transformation

Related

Image4io and NodeJS: Documentation is clearly outdated. How do I upload an image?

I guess this is as much a call to attention on the fact that the documentation seems to be outright incorrect in its application, as well as just generally lacking. Normally, I would send that part of the message to them personally, but, their contact form is also giving me errors and not sending, so it's not like I have the means to anyway. In the mean time, I'm more worried about getting this code to work, so hopefully someone experienced with this SDK or an Image4io team member sees this and can provide a public answer for others stumbling into this road block.
For starters, I initialized the Image4io object as described in the github here: https://github.com/Image4IO/image4ionodeSDK/
website documentation: https://image4.io/en/documentation/api-sdk/#operation/UploadImage
The image upload example provided on the website's documentation looks like this:
let client=new Image4ioAPI.Image4ioAPI(apiKey,apiSecret);
var request = new Models.UploadImagesRequest("/folderName", true, true);
request.Add("/path/to/image/location/name-of-the-image.jpg", "name-of-the-image", "name-of-the-image.jpg");
let response=client.UploadImage(request);
3 out of the very sparse 4 lines of code they provide give errors. Where did this Models object come from? There is no corresponding class in the import and the code example obviously doesn't show where it was defined. Just what is it and where did it come from?
Well, I found a matching function "UploadImagesRequest" in the original import class, so my guess is Models was deprecated and its functionality was moved into the Image4io class object. If that was the case the solution would be to simply access that function instead... But it's not used the same. It has 4 arguments, the 4th being a "Image4io.UploadFile[]" type. This type has no use examples in the documentation or further details describing what it is exactly. I assume image byte data goes in there somehow, but how?
Finally we have request.Add... except we don't because that isn't a function :( it looks like this was used to actually get the image data (maybe of the type UploadFile?) based on the path of the image. If this function is gone now, how do get file data for use in the upload request function?
Any and all help in figuring out this SDK would be greatly appreciated. Google searches yielded no meaningful results, so hopefully we can help in that department as well. For all I know I just got the wrong version somehow. I guess I could try downgrading to a version that matches the documentation but... that's not a fix in my eyes at all.
Let me know if there's any more info I could provide to help
You can upload image like this:
var client=new Image4ioClient(API_KEY,API_SECRET);
var files=Array();
files.push(new UploadFile("./test.jpg","test.jpg"));
client.UploadImage(new UploadImagesRequest("/",true,false,files))
.then(res=>console.log(res))
If you have binary data at hand, you can write it to a temporary file and then upload them.

How to allow output onto webpage from console log mqtt message

A few questions and I apologise if they seem unprofessional, I am a beginner programmer. But I would really appreciate any and all help you'd provide!
Currently, I have used an MQTT program to receive messages over websockets. I can view my message in the console.log but I would like to ask how can I convert it to the html output messages. is it by document.getElementById ? or is there a more better way to do that?
What is the benefit of using JSON for the messages?
What is the purpose of using variable.push(entry.anothervariable) is the push entry better and perhaps I should use an array to store the messages and then push them out?
Thank you!
Without context answering these questions are not really a good fit for Stackoverflow, but here are some answers.
Without the context of what frameworks you are using, yes finding an element with document.getElementById() and then updating it's content with .innerText or .innerHTML is probably the simiplest way forward
Sending messages encoded as JSON when working with JavaScript means that you don't need to worry about parsing any other encoding format, you can directly access fields using the native JavaScript Object Model.
This question is totally unanswerable with out any context for what you are actually working with. The .push() method is how you append an item to an existing array.

Variable keeps turning into an object instead of an Array in Javascript

I'm learning javascript (and HTML on Electron), and I have a variable which is meant to be an array:
var arrayList = [];
When I add objects to it (taken from JSON data), using Push:
arrayList.push(object);
Everything works perfect. I then save this using an api from npm called: electron-json-storage
I then want to pull this data, push an object into it and save it back. The problem is when I use the api to obtain the data to save into the variable arrayList, it turns arrayList into an object, it then errors out when I try to push to it as it is the wrong variable type now compared to before.
Would love any help and guidance would be great, thanks!
apologies about this, turns out the 'electron-json-storage' api sends back a blank object if the file doesn't exist, which was actually my problem (even though I know I didn't write that above) and what kept turning it into an object. I wouldn't of figured it out without re-looking over my code before posting here (so thanks klugjo) and for trying to parse the object which double confirmed it was an object and lead me to find the actual problem (So thanks so much Robert).
Again thanks all for the help!

VB Collection in JavaScript

I've been serching the net for quite some time and maybe there is no solution for my problem but I hoped you might be able to help me. I'm currently trying to establish a connection to CatiaV5 via JavaScript. The connection itsself is working just fine. Basically it looks like this now:
var catia = new ActiveXObject('CATIA.Application');
var doc = catia.ActiveDocument;
But here's my problem. The returned object is a Collection in VB and there seems to be no DataType equivalent to this. So this is what i get when i try to read my variables. For example:
doc.Product
returns
[object] {};
So this seems to be empty. However if then I try to get
doc.Product.Name
which by that logic should be undefined, instead i get
"Part1"
so the correct name of my Part/Product is returned.
All the Catia stuff probably isn't that relevant for the question.
my question: Is there any way to somehow parse a VB-Collection on a JavaScript object or something similar, to get the contents of what is returned?
OK,
here's the thing. I found out, that you can't really browse throug ActiveXObjects directly in the console in general. In the IE-Dev-Tools i was however able to use the Locals-Watch to browse through the Object. I didn't find a nice way to Parse it onto a JSON-Object, however the manual way to create a new Object and add the Keys piece by piece works.

How can I read a value from a webpage?

I ask now apologize for the issue that I will do, but already searched everywhere and found nothing relevant.
I need to read a data from a website, but not even managed to do.
The information is as follows:
And I need to read the numbers.
Did I could help please?
Thank you all.
If you need I clarify my doubts.
Very vague and unclear but it looks like json data so... how is this data retrieved? Is this a simple webpage loaded in a browser or the result from some ajax / rest request?
You could try using DOMDocument and then searching the DOM for the PRE tag and getting the contents that way.
If it is a json response from an ajax call then it is a trivial task. More info is required to get a sensible answer

Categories

Resources