名前がデータセット内の名前とまったく同じではないにもかかわらず、ユーザーが名前を入力してコードを機能させるようにしたいとします。例:「Camden」の入力は機能しますが、「camden」の入力は機能せず、エラーが返されます。入力クエリに寛容を許す方法は?
コードを作成するには、たとえば、大文字でない文字や間違った文字を使用できますか?
app.get("/api/borough_by_name", function(req, res){
// set the type of content in the response
res.writeHead(200, {'Content-Type': 'application/json'});
// read input parameter from request
var name = req.query['name'];
// validate input, check if the parameter is valid or not
if (name === undefined){
// return error as a JSON string
var str = JSON.stringify({"error":"Parameter 'name' is not defined."});
res.end(str);
}
var borObj = null;
// loop over the statesData GeoJSON structure
for (i in londonBoroughs.features){
var feat = londonBoroughs.features[i];
if (feat.properties.NAME == name){
borObj = feat;
continue;
}
}
if (borObj == null){
// borough not found, return an error as a JSON string
var str = JSON.stringify({"error":"NAME '"+name+"' doesn't exist. Please check the spelling."});
res.end(str);
}
var respJson = {};
respJson["results"] = [ borObj ];
res.end(JSON.stringify(respJson));
あなたは「寛大」のためのあなたの基準は正確に何で自分自身のために決定する必要があります。 – Pointy