JavaScript object has different value in source page than in console - javascript

So my application is wrote in java and Spring and I need to pass a hash map with [data,value] from backend to frontend , using THYMELEAF, so I can render them into a graph. These are my lines in my controller
model.addAttribute("hashValues",resultHash.values());
model.addAttribute("hashTime",resultHash.keySet());
When trying to access them in javascript, the hashValues seem to be right, but the time list has a problem. When using console.log to print hashTime my console says something like [1996,1997,1997] when in reality my dates are [2021-10-15, 2021-10-14, 2021-10-13]. I will add some images for a better understanding.
This is how I get the lists from the model.
var time = [[${hashTime}]]
var values = [[${hashValues}]]
console.log(time)
console.log(values)
I've tried using JSON.parse but I have the same problem. Any help is appreciated cause I've been working with JavaScript for some time and I never encountered this type of problem before.

Related

Access Blazor object[] from Javascript without passing data through unmarshalled call

Just want to highlight that I have been at this for about a week and am new to dealing with the heap and pointers and have not had to interact directly with it before. Could really use some help as I have not made any progress.
Problem statement: Need to access an object[] that I created on Blazor side from Javascript without the use of JSUnmarshalled call from Blazor to pass the data in.
Why? Using the JSUnmarshalled call is still too slow for how large the object[] is even though it only takes .25ms (yes .25ms) to actually create the entire array. The array takes 2 seconds to pass to JS. This is a problem because I am rapidly making adjustments based on user input and most time is spent on waiting for the data to be passed over which causes poor user experience.
What I am currently doing:
c# side
JSUnmarshalledRuntime.InvokeUnmarshalled<object[], object>("InitializeGeometry", RenderModel.GetCombined());
InitializeGeometry calls this JS function to process the object[]
JS side
function getInt32Heap(data) {
var m = data + 12;
var r = Module.HEAP32[m >> 2]
return Array.from(new Int32Array(Module.HEAP32.buffer, m + 4, r));}
This is working fine but is quite slow. What I have tried to find is a way to create a pointer for the object[] on the Blazor side and then pass it into JS. Then on the JS side figure out how to use that pointer to get the same data out that I am currently getting.
I have reached out to the .Net team and this is my original question.
https://github.com/dotnet/aspnetcore/issues/41490
Here is what I have looked through
https://github.com/dotnet/aspnetcore/blob/main/src/Components/Web.JS/src/Rendering/RenderBatch/SharedMemoryRenderBatch.ts
Also found this
https://github.com/SteveSandersonMS/BlazorInputFile/blob/db10236b049ea05de28de9b7806447a3c84c8465/BlazorInputFile/wwwroot/inputfile.js#L113
And this
https://blog.elmah.io/using-js-object-references-in-blazor-wasm-to-wrap-js-libraries/?msclkid=9826568bcf8d11ecbdab91d116441786
Again, I am new to this so the answers may be in here but I am not seeing them. I know this is due to my lack of understanding but so far I have not read anything that puts things into perspective for me. Any help would be greatly appreciated!

How to remove the html generated from razor var to Javascript variable?

I'm using MVC5 and passing model from Controller to View. In the view I have
var test = #Html.Raw(Json.Encode(Model.Data));
It work great, I have my model in the js variable. The problem is when I'm going to see my HTML code, the whole variable is rendered
Is there any way how I can remove or hide this code?
Thanks
To pass a variable from your server to the client you have two options really:
Do what you have done (but the value is rendered in markup)
Expose a webapi and query the endpoint in javascript to get the data
Option 2 is best practice.
Without more details on what you're trying to achieve I can't give a more detailed answer, but basically you should be looking into webapi to resolve this.
note: there are other things you could do like websockets and cookies, but it's unlikely that you want that kind of stuff for this

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!

Is it possible to use express-handlebars tags inside an HTML script tag on the client side?

I have been struggling with this issue for quite some time now. I'm creating an application using express-handlebars and the biggest part of the application works well. Most of the data I want to display on the webpages I'm making is displayed well and I am taking good use of the Helper functions of the express-handlebars module to do so.
Now, the part where I'm stuck is the following. In one of the HTML templates I use in my application, I am using a tag to display a map with several locations on it using the Google Maps JS API. These locations are coming out of a MongoDB database, just like all of the other template-data I am showing on this page.
The data is coming to the client side here as an array of objects over which I want to iterate, filter out the addresses, geocode them to the correct format and display them in the Map. The problem here is that I can get this array into the page just fine, but I can't get inside of the as an object through which I can iterate.
Is there any way I can get this array of locations inside the script tags, so I can iterate over them to display them inside of the Map view?
You should be able to do something like this within the template itself:
var data = JSON.stringify(location_data)
var script = 'var data = ' + data
And then inside of your script tag:
include the 'script' variable
write the rest of your client-side code
I'm not super familiar with handlebars syntax, but I know that you can do something similar using express and pug :)
That said, it might just be easier (and cleaner) to request the map data on the client side using an xmlhttp request/ajax?

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.

Categories

Resources