I'm creating a dummy JSON value and is trying to show them but the problem is i keep getting this error:
My code snippet is below:
var jsonData = '[{"id":"31370100","machine_name":"GUMACA BRANCH CAM","machine_type":"CAM","operation_start":"05:04:33","operation_end":"09:04:33"}]'
d3.json(jsonData).then((data)=>{
console.log(data);
});
d3.json takes a URL as the parameter. As you have the JSON already, you probably just need JSON.parse(), and there is no need to use d3 in this instance. So your code would look something like:
var jsonData = '[{"id":"31370100","machine_name":"GUMACA BRANCH CAM","machine_type":"CAM","operation_start":"05:04:33","operation_end":"09:04:33"}]'
let data = JSON.parse(jsonData);
console.log(data);
Related
Using the JavaScript SDK, what kind of object would be returned on the following query:
var groupQuery = new Parse.Query('some_valid_class');
var group = groupQuery.get('some_valid_object_id');
console.log(group);
I only see [object Object] in the log.
thanks!
On Cloud Code - You can't print objects directly as we usually does in console of browser.
You have to use console.log(JSON.stringify(yourObject))
Although the documentation doesn't say so, I believe the get method returns a Promise. Here is an example for getting the actual object taken from the documentation:
var query = new Parse.Query(MyClass);
query.get(myId, {
success: function(object) {
// object is an instance of Parse.Object.
},
error: function(object, error) {
// error is an instance of Parse.Error.
}
});
In the success-function a Parse.Object is provided.
You can find all the info at the Parse JavaScript SDK & Cloud Code Reference
Specific Parse.Query.get() documentation here: https://parse.com/docs/js/symbols/Parse.Query.html#get
I am trying to figure out how Dynamic Websites on Parse work.
I have followed the instructions here: https://parse.com/docs/hosting_guide#webapp to set up a basic example.
Beside cloud/views/hello.ejs, I have made cloud/views/mything.ejs and used that from app.js and it all works.
Now I would like to show for example the number of records in MyClass on Parse.
In other words, inside my Dynamic Website I want to display information related to the contents of the DB on Parse.
How can I do that? Obviously I need to include some DB query at some point, but is there any sample?
Here's an example snippet
This handles a GET request to /mypage, on execution it creates a Parse Query but returns a result count instead of a record set matching the query. On completion of the query we then set a count or error on the response using res.set(). You can then use ejs to display the count or error.
app.get('/mypage', function(req, res) {
var query = new Parse.Query('My Class');
query.equalTo("name", "Joe Blogs");
query.count({
success: function(count) {
res.set('count', count);
},
error: function(error) {
res.set('error', error);
}
});
});
I get a response from an ajax request and store request.responseText in a variable called requestData. requestData data contains a json object pass by php using json_encode().
See a couple of log I made bellow.
requestData : {"status":"ok","to":"","html":"<option value=\"Huberdeau\">Huberdeau<\/option><option value=\"Bo\u00eeleau\">Bo\u00eeleau<\/option><option value=\"Br\u00e9beuf\">Br\u00e9beuf<\/option><option value=\"Saint-R\u00e9mi-d'Amherst\">Saint-R\u00e9mi-d'Amherst<\/option><option value=\"Harrington\">Harrington<\/option>","message":"old"}
My probleme is that I can't access the variable status. None of the following calls get the value properly.
requestData[status] : undefined
requestData.status : undefined
requestData['status'] : undefined
requestData[0]['status'] : undefined
And when I tried to dump my variable by a for log it treated it like string.
dump_var :
0:{
1:"
2:s
3:t
4:a
5:t
6:u
7:s
8:"
9::
in jQuery
instead of $.get
use $.getJSON
$.getJSON(url, function(data){
alert(data.someField);
});
then you don't need to do eval !!
I have found the trick in a comment on an other question. I post it here anyway cause it's a bit difficult to notice in the original discussion.
var requestData = request['responseText'];
var jsonData = eval("( " + requestData + ")");
What Im trying to do with my Greasemonkey script is:
to read some distant XML document;
convert it into XML object;
and then use XPath to get the elements inside of it.
The getElementsByTagName(TagName) method works fine with my XML-object, but evaluate("XPath expression") doesn't. Any suggestions? See the code below:
GM_xmlhttpRequest({
method: "GET",
url: "http://www.someserver.com/atom.xml",
onload: function(response) {
if (!response.responseXML) {
var xmlDoc = new DOMParser().parseFromString(response.responseText, "application/xml");
}
// this section works fine and returns the data of the first <entry>..</entry>
var snapEntries = xmlDoc.getElementsByTagName("entry");
alert (snapEntries[0].data);
// this section doesn't work for unknown reason and returns nothing
var snapEntriesXpath = xmlDoc.evaluate("//entry", xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
alert (snapEntriesXpath.snapshotItem(0).data);
}
});
IIRC, the .data attribute won't be present for every kind of search.
You probably need to use:
var snapEntriesXpath = xmlDoc.evaluate (
"//entry//text()", xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
);
But several other issues could be at play. If that doesn't do it, (1) link to the exact XML file; use pastebin.com if necessary. (2) Report what Firefox's error console (CtrlShiftJ) reports.
In the function getweather() I do fetch some weather data from a Json-object and store that data in the variable data. Within that function, I can handle the Json-data, but how do I access the data from outside of getweather()?
It doesn't matter if i return the variable location or data. The variable place is just not Json.
How do I handle the place variable in order to make it work as it does within the function getweather()?
var ajax = require('ajax');
var place;
place = getweather();
function getweather()
{
// Make the request
ajax(
{
url: 'http://api.openweathermap.org/data/2.5/weather?q=paris.fr', type: 'json'
},
function(data)
{
// Success!
console.log("Successfully fetched weather data!");
// Extract data
var location = data.name;
return location;
},
function(error) {
// Failure!
console.log('Failed fetching weather data: ' + error);
}
);
}
The A in AJAX stands for asynchronous. What's happening in your code is that you are trying to assign a value to place before the asynchronous call to api.openweather.org has returned.
You can check this by placing a statement like console.log(place); directly after place = getweather();. You will notice in the console that None is returned before you see Successfully fetched weather data!
This issue has been described in detail in this post. It recommends restructuring your code around your callbacks.