我使用javascript和REST API查询Neo4j数据库,按名称搜索节点。提交查询后,firebug控制台显示错误400“错误请求”,其中包含以下内容: “message”:“你必须提供'查询'参数。”, “exception”:“BadInputException”,
以下是我用来提交查询的功能。当警告“search_query”时,语法似乎是正确的,并且字符串化的“queryObject”是有效的JSON。谢谢你帮助我理解为什么会这样,以及如何解决它。
~~~~ 注意:只需使用以下方法即可使用:
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>
答案 0 :(得分:0)
数据类型json自己进行转换,因此您必须在那里提供对象。正如你所注意到的那样。
我强烈建议您在查询中使用n.name =~{search_name}
参数,然后传递
{query: ...,params: { search_name: search_name+".*"}}