I am querying a Neo4j database using javascript and the REST API to search for a node by name. Upon submitting the query the firebug console shows an error 400 "Bad Request" with the following: "message" : "You have to provide the 'query' parameter.", "exception" : "BadInputException",
Below is the function I am using to submit the query. When alerting on "search_query" the syntax appears to be correct and the stringified "queryObject" is valid JSON. Thank you helping me understand why this is happening and how to fix it.
~~~~ Note: Just got this to work by using:
data:{
"query":"start n = node(*) WHERE n.name =~ '" + search_name + ".*' return n order by n.name asc",
"params":{}
},
~~~~
<script>
function name_search()
{
var queryObject = new Object; //declare object to hold query and parameters
var search_name = document.getElementById("name_search").value; //get node name search term from user input
search_name = "'"+search_name+".*'"; //append ".*" to search on Regular Expression
//alert("search: " + search_name);
search_query = 'start n = node(*) WHERE n.name =~ ' + search_name + ' return n order by n.name asc'; //create query
queryObject.query = search_query; //insert query string in queryObject
queryObject.params = {}; //empty object = no query parameters
alert(JSON.stringify(queryObject));
var restServerURL = "http://localhost:7474/db/data"; //local copy on windows machine
$.ajax({
type:"POST",
url: restServerURL + "/cypher",
accepts: "application/json",
dataType:"json",
data:JSON.stringify(queryObject), //convert queryObject to JSON for inserting into database
success: function(data, xhr, textStatus){
//process query results
$('#query_results').empty(); //clear div that will contain results
var length = data.data.length; //capture number nodes returned
//alert("number of nodes: " + length);
$('#query_results').append($('<p>').html('number of nodes: ' + length + '<br />'));
for (var u = 0; u < length; u++){
var num_props = Object.keys(data.data[u][0].data).length;//get number of node properties from length of data.data.data child property in JSON
var node_num = data.data[u][0].self;//get node number from data.data.self and
node_num = node_num.replace(restServerURL+"/node/","");//strip restServerURL+"/node/" from result
//alert("Node "+ node_num + " has: "+ num_props + " properties");
$('#query_results').append($('<p>').html('Node '+ node_num + ' has: '+ num_props + ' properties' + '<br />'));
for (var v = 0; v < num_props; v++){
var prop = (Object.keys(data.data[u][0].data))[v];//get property name key
var val = data.data[u][0].data[prop]; //use property name to get value
//alert("prop: " + prop +" value: " + val);
$('#query_results').append($('<p style="text-indent: 1em;">').html('prop: ' + prop +' value: ' + val + '<br />'));
}
};
},
error:function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
}
});
}//end of search for node by name
</script>
datatype json does the conversion itself, so you have to provide the object there. As you noticed as well.
I would strongly recommend that you use a n.name =~{search_name}
parameter in your query and then just pass
{query: ...,params: { search_name: search_name+".*"}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.