I'm having a hard time determining why this query works in the Mongo shell but not when I try to hit the server endpoint from the browser.
Shell command:
db.playlists.find({keywords: {"$in" : ["featured"]}}).limit(5)
Server code:
app.get('/getFeaturedPlaylists', (req, res) => {
let query = {
keywords: { "$in" : ["featured"]}
}
database.Playlist.find({query}).limit(5).exec(function(err, data) {
err ? console.log(err) : res.send({body: JSON.stringify(data)});
})
})
Expected result is an array of playlist objects:
{"body": "[{\"songList\":[{\"songId\":522,\"bpm\":45},{\"songId\":310,\"bpm\":45},{\"songId\":199,\"bpm\":90},{\"songId\":170,\"bpm\":150},{\"songId\":17,\"bpm\":150},{\"songId\":722,\"bpm\":45},{\"songId\":767,\"bpm\":90},{\"songId\":754,\"bpm\":120},{\"songId\":844,\"bpm\":150},{\"songId\":422,\"bpm\":120},{\"songId\":638,\"bpm\":90},{\"songId\":243,\"bpm\":150},{\"songId\":524,\"bpm\":90},{\"songId\":575,\"bpm\":120},{\"songId\":143,\"bpm\":120}],\"shuffledQueue\":[],\"keywords\":[\"bluegrass\",\"featured\"],\"_id\":\"5a76188fde76d97bda5e342e\",\"playlistId\":2,\"playlistName\":\"Playlist2\"},{\"songList\":[{\"songId\":401,\"bpm\":45},{\"songId\":536,\"bpm\":150},{\"songId\":982,\"bpm\":60},{\"songId\":812,\"bpm\":60},{\"songId\":466,\"bpm\":150}],\"shuffledQueue\":[],\"keywords\":[\"featured\",\"bluegrass\"],\"_id\":\"5a76188fde76d97bda5e3432\",\"playlistId\":6,\"playlistName\":\"Playlist6\"},{\"songList\":[{\"songId\":248,\"bpm\":120},{\"songId\":84,\"bpm\":45},{\"songId\":53,\"bpm\":60},{\"songId\":749,\"bpm\":45},{\"songId\":811,\"bpm\":120}],\"shuffledQueue\":[],\"keywords\":[\"featured\",\"hip-hop\"],\"_id\":\"5a76188fde76d97bda5e3433\",\"playlistId\":7,\"playlistName\":\"Playlist7\"},{\"songList\":[{\"songId\":894,\"bpm\":150},{\"songId\":190,\"bpm\":60},{\"songId\":235,\"bpm\":60},{\"songId\":632,\"bpm\":60},{\"songId\":970,\"bpm\":60},{\"songId\":475,\"bpm\":90},{\"songId\":304,\"bpm\":60},{\"songId\":816,\"bpm\":120},{\"songId\":613,\"bpm\":150},{\"songId\":310,\"bpm\":45},{\"songId\":599,\"bpm\":45},{\"songId\":91,\"bpm\":90},{\"songId\":650,\"bpm\":120},{\"songId\":219,\"bpm\":45},{\"songId\":290,\"bpm\":90}],\"shuffledQueue\":[],\"keywords\":[\"hip-hop\",\"featured\"],\"_id\":\"5a76188fde76d97bda5e3436\",\"playlistId\":10,\"playlistName\":\"Playlist10\"},{\"songList\":[{\"songId\":778,\"bpm\":60},{\"songId\":652,\"bpm\":60},{\"songId\":792,\"bpm\":90},{\"songId\":353,\"bpm\":120},{\"songId\":528,\"bpm\":60},{\"songId\":887,\"bpm\":120},{\"songId\":287,\"bpm\":90},{\"songId\":926,\"bpm\":120},{\"songId\":671,\"bpm\":90}],\"shuffledQueue\":[],\"keywords\":[\"featured\",\"reggae\"],\"_id\":\"5a76188fde76d97bda5e3437\",\"playlistId\":11,\"playlistName\":\"Playlist11\"}]"}
Actual result is an empty array:
{"body":"[]"}
Even more peculiar, this issue only arose when I moved the server and database to EC2 instances. Using the exact same code and data set on my local machine, it responds as I expect it to (via Postman or Chrome -- this is how I got the "expected response" snippet above). Additionally, all of the other server endpoints pointing at this database work, but they're only looking for individual playlists.
Any insights into this would be greatly appreciated, and of course I'm happy to provide any more details as needed.
Thanks!
Here's the issue,
database.Playlist.find({query})
You're not passing the query object but rather an empty object { } with {query}.
Do this instead,
database.Playlist.find(query)
Hope this solves the problem.
I'm trying to get data from OBDII using ionic native bluetooth plugin. However, when i called write method to send the command to the device, no data was returned. The code I used is as below:
readData(device){
this.bluetoothSerial.write('010D').then( (success) => {
alert('Connected to ' + device.name + '. Data reading is successful: ' + new Uint8Array(success));
},
(error) => {
alert('reading failed:' + error );
});
}
The result is shown as below:
My question is: what is the proper way to send command to OBDII to retrieve data using native plugin.
Try with "010D\r" where \r is a carriage return....and read the Hex response.
If you receive NoData means that or the sensor is not in the car or you send an incorrect command.
Remember that the ELM327 can manage one command per time, so you must use something like Queue for manage multi command.
PS:read some documentation about ELM327 and how it manage commands and configuration
I'm trying to load a XOD document into a PDFTron WebViewer. As far as I can read in the documentation and samples, this should be a simple "plug and play"-operation - it should simply work when you point at a file. Ideally, in my example, the document should be fetched from a service, as so:
fetch('/myservice/GetXOD')
.then(function(data) {
$(function() {
var viewerElement = document.getElementById("viewer");
var myWebViewer = new PDFTron.WebViewer({
initialDoc: data.body
}, viewerElement);
});
});
Unfortunately I get the following error:
Uncaught TypeError: Cannot read property 'DisplayModes' of undefined
The reason I'm doing it in a fetch, is because I'm rendering a Handlebars template, and pass the data to instantiate in a callback. However, I've isolated the code into an otherwise "empty" HTML-document, and in the simplified example below, I'm simply pointing at the XOD provided by PDFTron on page load (no fetch this time).
$(function() {
var viewerElement = document.getElementById("viewer");
var myWebViewer = new PDFTron.WebViewer({
initialDoc: 'GettingStarted.xod' // Using the XOD provided by PDFTron
}, viewerElement);
});
This unfortunately returns a different error (HTTP status 416).
Uncaught Error: Error loading document: Error retrieving file: /doc/WebViewer_Developer_Guide.xod?_=-22,. Received return status 416.
The same error appears when I run the samples from PDFTron on localhost.
I'm at a complete loss of how I should debug this further - all the samples assume everything is working out of the box.
I should note that I can actually get PDFs working just fine on localhost, but not on the server. XODs are problematic both on the server and on localhost.
I'm sorry to hear you are having troubles running our samples.
Your error message says 416 which means "Requested range not satisfiable". Perhaps your development servers do not support byte range requests (https://en.wikipedia.org/wiki/Byte_serving).
Could you try passing an option streaming: true? When streaming is true you're just requesting the entire file up front which shouldn't be a problem for any servers but it is a problem for WebViewer if the file is large because it will need to be completely downloaded and parsed at the start which is slow.
I have a Sparql Endpoint(openrdf sesame) on my local machine. I am able to get the results of the queries(6) in json (using sparql-client node module). Now, I need to convert this json into json-ld so that I can easily display it in the jade file. How can I do this without using rdfstore-js in my node.js application? I did read about jsonld node module. The json data is quite huge and so I cant specify the context for it.
Problem with rdfstore-js:
Since the endpoint is on my local machine, I am facing the cross domain issue. So, I tried to load data from remote sparql endpoints into the store but it does not work.
https://github.com/antoniogarrote/rdfstore-js/issues/20
Alternatively, I am open to suggestions wherein I can display data without json-ld.
Edit: After reading Gregg' suggestion, I tried to use CONSTRUCT query. However, the sesame server
returns response of the query as text/plain. I tried the content negotiation by giving accept header for json-ld. But it would fail - gives me 'No acceptable file format found'.
Thanks!
I am now using http nodejs module as below. I get the response as text/plain. Is there a way to convert plain text into json-ld ?
var queryString = syllPrefix+" CONSTRUCT { ?uri ?p ?o } where { ?uri a syll:Person . ?uri ?p ?o}";
var encodedquerystring = encodeURIComponent(queryString);
var options = {
host: 'localhost',
port: 8080,
path: '/openrdf-sesame/repositories/myrepo?query=' +encodedquerystring,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/plain',
},
};
var req = http.get(options, function(res) {
// console.log("Got response: " + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
console.log (data);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
req.end();
Sesame does not support the JSON-LD format out of the box. You will need to install a Sesame-compatible JSON-LD writer library (such as jsonld-java) into your existing Sesame installation.
To do this, get the jsonld-java and jsonld-java-sesame jar files plus their dependencies (unfortunately this is a bit of a chore as you will need to include several third party dependencies, such as Apache libraries and Jackson, as well - but you can download everything from Maven Central) and drop them into your Sesame deployment directory inside Tomcat. Typically this directory should be something like [TOMCAT_DIR]/webapps/openrdf-sesame/WEB-INF/lib. Once you've done this, you'll need to restart Tomcat for Sesame to recognize the new writer.
Once this is done, supplying the correct Accept header (media type application/ld+json) should get you JSON-LD results on your graph queries.
Alternatively, you can try using the RDF 1.1 JSON Alternate Serialization format, which is not really a standard like JSON-LD is, but is supported by Sesame. Media type application/rdf+json.
To get JSON-LD from sparql-client (at least the Ruby version), use CONSTRUCT or DESCRIBE rather than SELECT or ASK. If you're using directly, you can then serialize the resulting graph as JSON-LD (on a platform supporting this, such as Python, Ruby or Java), or if it's a remote connection, an HTTP service should allow you to content negotiation for the results using application/ld+json.
Turning SELECT or ASK into JSON-LD is more problematic, and would require a result-set vocabulary. The application/sparql+json is not easily directly interpreted as
I've been using Google Translate API for a while now, without any problems.
I recently pushed my app to my new server and even if it has been working perfectly on my local server, the same source code always gives me the "Required parameter: q" as error message.
I'm using NodeJS + ExpressJS + Request to send this request. Here's my test case:
var request = require('request');
request.post({
url: "https://www.googleapis.com/language/translate/v2",
headers: {"X-HTTP-Method-Override": "GET"},
form: {
key: /* My Google API server key */,
target: "en",
q: ["Mon premier essai", "Mon second essai"]
}
}, function(error, response, data) {
if (!error && response.statusCode == 200) {
console.log("everything works fine");
} else {
console.log("something went wrong")
}
});
Running on my local machine gives me "everything works fine", and running it on my server gives me "something went wrong". Digging more into it, I get the error message mentioned above.
As you can see, I'm trying to translate in one request two sentences. It's just a test case, but I really need to use this through POST request instead of doing two GET request.
I have no idea what this is happening, and I double checked my Google settings and I can't find something wrong there.
Also, I'm having no problem using Google Places APi with this same api key on my server.
I'm stuck. Anyone has any idea what's wrong here?
Well I finally found what was wrong: the new version of RequestJS doesn't work as the old one and my server was running 2.16 when my local machine was running 2.14.
The difference is the way the array is sent. I debugged and the old version was sending
key=my_api_key&target=en&q=Mon%20premier%20essai&q=Mon%20second%20essai
When the new version is sending
key=my_api_key&target=en&q[0]=Mon%20premier%20essai&q[1]=Mon%20second%20essai
So I just added 2.14.x instead of 2.x in my package.json file for now, hopefully it will get fixed soon — or maybe it's not a bug? I don't know.
This answer is a little late but to help people out there with this problem. The problem comes from the way the querystring module converts array parameters:
https://github.com/visionmedia/node-querystring
Its function qs.stringify converts fieldnames (q in the given example) that have an array value to the format:
q[0]=..q[1]=...
This is not a bug but an intended functionality. To overcome this problem without reverting to an old version of the request module you need to manually create your post by using the body option instead of the form option. Also you will need to manually add the content-type header with this method:
var request = require('request');
request.request({
url: "https://www.googleapis.com/language/translate/v2",
headers: {
"X-HTTP-Method-Override": "GET",
'content-type':'application/x-www-form-urlencoded; charset=utf-8'
},
body:'key=xxxx&target=en&q=q=Mon%20premier%20essai&q=Mon%20second%20essai'
}, function(error, response, data) {
if (!error && response.statusCode == 200) {
console.log("everything works fine");
} else {
console.log("something went wrong")
}
});
Obviously this is not as clean but you can easily create a utility function that creates the body string from the object the way you want it to.
things that pop into my head:
jquery file version on server and local PC are not the same
file encoding issues (UTF8 on PC ascii on server?)
have you tried testing it with chrome with Developer Tools open, then check "Network Tab" and verify exactly what is being sent to Google.
For me at least, when it works on one machine and not the other, it is usually due to the first 2 options.
Good Luck!