By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!But can Node.js be used in the browser ? Because I think that is the situation here, a webpage reading something from a website and wanting to store it.A fast read for the file system in node.js https://www.w3schools.com/nodejs/nodejs_filesystem.asp
And here is the official documentation https://nodejs.org/api/fs.html
the node.js can be used with the MySQL, SQLlite, or postgreSQL
@OldMan Do you know how long it takes to learn it because my time is limitedA fast read for the file system in node.js https://www.w3schools.com/nodejs/nodejs_filesystem.asp
And here is the official documentation https://nodejs.org/api/fs.html
the node.js can be used with the MySQL, SQLlite, or postgreSQL
I created a page on the local machine that uses an API to fetch the data from a web platformBut can Node.js be used in the browser ? Because I think that is the situation here, a webpage reading something from a website and wanting to store it.
Ah ok. If you are using Node.js to get your data there should be no problem saving it 🙂I created a page on the local machine that uses an API to fetch the data from a web platform
Do you know how long it takes to learn node.js from scratch? and is its structure very different than the JS?Ah ok. If you are using Node.js to get your data there should be no problem saving it 🙂
I don't know what it would mean to "learn node.js from scratch". Depends on your experience, and how much you want to learn. If you know JS it should not be a big deal. Language-wise they are pretty much the same, but differ in some capabilities. See for example https://www.javatpoint.com/javascript-vs-nodejsDo you know how long it takes to learn node.js from scratch? and is its structure very different than the JS?
function hostCPU() {
sum1=0;
count1=0;
fetch("url....")
.then((response) => response.json())
.then((data) => monitorHostCPU(data))
.catch(() => renderError());
}
function monitorHostCPU(data) {
// Get text elements
const displayName = document.getElementById("displayName");
const dataPoints = document.getElementById("dataPoints");
const error = document.getElementById("error");
result=Object.values(data.dataResult.dataPoints);
resultStr=String(result);
resultArr=resultStr.split(',');
for(i=0; i<resultArr.length;i++){
if (resultArr[i]!=="")
if (i%2==1){
sum1+=Number(resultArr[i]);
count1+=1;
}
else{
//str+="|" ;
sum1+= 0;
}
}
average1=sum1/count1;
list.push({metricName:data.displayName, metricValue:average1});
error.innerHTML ="";
res.innerHTML = average1;
disName.innerHTML = "Host CPU Usage";
if (average1>500 ){
res.style= "color:#f00;";
}
}
Yes, I can save one value every time I refresh the page. using the if statement I told if localStorage is empty setItem but if it's not empty push the new average into the array. it doesn't return any error but it doesn't push the new value either. So I just changed my mind and I might use node.js or write the entire code in python or phpThanks, interesting. I did not know about the fetch API.
Can you explain your last sentence in more detail ? If I understand it correctly you can save individual values but have trouble saving an array ? How exactly are you doing both ? And what happens when you try to save the array ? Do you get an error in the console ?
list.push({metricName:data.displayName, metricValue:average1});
list
before and after the push and have you checked the console log in the debugger ? I seem to be asking this last question in every post 🙄🙂 Also not sure what you mean with localStorage
being empty and setItem
. I don't see any of that in the code you posted. Details, details....Personally I would try to solve that problem rather than switching to another language. I'm not sure how exactly you "push the new average into the array", and why that does not seem to work. If this does not work in JS I rather doubt that it will work in Node.js. I guess you are talking about this statement
list.push({metricName:data.displayName, metricValue:average1});
Have you examined the value oflist
before and after the push and have you checked the console log in the debugger ? I seem to be asking this last question in every post 🙄🙂 Also not sure what you mean withlocalStorage
being empty andsetItem
. I don't see any of that in the code you posted. Details, details....
But ok, it's you call if you want to try some other language or JS variant.
exist=JSON.parse(localStorage.getItem('cpu')) || [];
if(exist) {
exist.push(average);
localStorage.setItem(cpu', exist);}
else
localStorage.setItem('cpu', average1);
localStorage
😳setItem
command ? And I ask once more, have you checked the console log ? Because there should have been an error.setItem()
expects a string as value, and you are passing it a list of more than one items. When that has been stringified, the result may not be what you expect.No problem.Interesting, thanks. I did not know aboutlocalStorage
😳
Did you notice the missing quote in the firstsetItem
command ? And I ask once more, have you checked the console log ? Because there should have been an error.
But I am not sure it would work even with the quote.setItem()
expects a string as value, and you are passing it a list of more than one items. When that has been stringified, the result may not be what you expect.
Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.