I tried to execute this code using functions at OrientDB Studio.
commandResult = db.executeCommand('insert into user (facebookId,instagramId) values ("' + facebookId +'","'+ instagramId +'");
if( commandResult.length == 0){
response.send(200, "ok", "application/json","{\"success\":\"false\"}");
} else {
response.send(200, "ok", "application/json","{\"success\":\"true\", \"#rid\" : \"" + commandResult + "\"}");
}
and then it returns like this
{"success":"true", "#rid" : "user#11:15{facebookId:df,instagramId:sdf} v1"}
My problem now is i want only to return only the rid value. But the problem is in my second key "user#11:15{facebookId:df,instagramId:sdf} v1". I don't know how am I going to parse it since the #rid is in the outside of the curly brace.
Hope from your positive response.
Thanks.
You're concatenating strings. Use the .toJSON() instead:
response.send(200, "ok", "application/json",
"{ \"success\":\"true\", \"#rid\" : \"" +
commandResult.toJSON() + "\"}");
Related
JS drives me insane with issues like this. I have the following code which creates a string (composed of session data and date information) to be written to an array, as such:
var _writes = String(req.session.subscriber + ":" + req.session.postal + "[" + req.session.id + "]=" + _onYear + "-" + _onMonth + "-" + _onDay + "-" + _onHour + "-" + _onMinute);
_users.push(_writes);
Later, I wish to perform an 'indexof' command on the string of the array, as such:
for (_cycle = 0; _cycle < _users.length; ++_cycle) {
_seeks = String(_users[_cycle]);
_score = _seeks.indexof("="); //ERROR THROWN HERE
//do other stuff here...
} //for loop
My error is "TypeError: _seeks.indexof is not a function"...? I thought by converting everything to a string I should be able to perform the 'indexof' command. Can somebody please advise what the issue is here? I thank you in advance.
Probably not a js issue. You are using "indexof" instead of "indexOf" (Uppercase O). Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
It should be:
_seeks.indexOf("=");
Don't give up, it will make sense soon :)
So i'm sending a String with javascript to a php page :
if(cp.value!=''){
s+=cp.name +" LIKE '%"+ cp.value +"%'";
console.log(s);
if(sec.value!=''){
s+=" AND "+sec.name+" LIKE '%"+ sec.value +"%'";
console.log(s);
}
}
else{
if(sec.value!=''){disappear
s+=sec.name+" LIKE '%"+ sec.value +"%'";
}
}
console.log(s);
if(s.length!=0){
var connect = new XMLHttpRequest();
connect.onreadystatechange=function(){
if (connect.readyState==4 && connect.status==200){
var resu=connect.responseText;
console.log(resu);
var tab=document.getElementById("main_tab");
tab.innerHTML=resu;
}
};
connect.open("POST","../../Controller/stage.php",false);
connect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
connect.send("s="+s);
}
}
The string sent is for exemple :
CP_Stage LIKE '%90%' AND secteur_stage LIKE '%ait%'
But when i print the request in the php page i have something like :
SELECT * FROM Stage WHERE CP_Stage LIKE '�%' AND secteur_stage LIKE '%ait%';
i have no idea why my first number disappear with the first %.
If anyone have an idea it would be awesome, thanks !
The percent-sign is a special charcter. Any special characters like %,&,? etc need to be encoded. Your "%90" is converted to an Ascii-Value. You have to encode these values with encodeURIComponent.
s += cp.name + " LIKE '" + encodeURIComponent("%" + cp.value + "%") + "'";
Note that encodeURIComponent does not escape the ' character. If your cp.value has an ' you have to replace it with its encoding value: %27.
By the way.. its a bad idea to send mySQL-queries from client-side - thats a major security flaw. Send only the values and build your queries on server-side.
I'm a total JS beginner. Here is the JsBin link formLetter test should be passing.
TL;DR
This:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
console.log(formLetter("Hermione", "Luna","How are you?"));
Should return:
"Hello Hermione,
How are you?
Sincerely,
Luna"
But instead I get this:
"Hello [object Object],
undefined
Sincerely,
undefined"
Edit
Sorry for the confusion. I'm working on different problems inside one JsBin. This is the correct JsBin with the isolated code.
This is because you are only getting one object passed into the function call. This object contains the information you need in lieu of the named arugments you have provided.
The first argument, recipient being [object Object] tells you that it's an object. undefined means that nothing was passed in their place. This signifies the common pattern of a config or param object being passed to the function call. Because of this, what you have as named arguments should really be property look ups on the object provided as the first argument.
Your function definition should look more like:
var formLetter = function (letter) {
// do something with letter
};
Inside of that function call, you may then hit the properties of the letter object to see if they contain what you need, Doing console.log debugging in dev tools will help track it down.
The line:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
in your example needs one semicolon after "sender", like:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender;
};
Your undefined is related to the use of anidated console.log.
You do:
console.log(longMessage.formLetter("Hermione", "Luna","How are you?"));
and (in the JsBin) you have also:
var longMessage = {
formLetter: function(recipient, sender, msg) {
console.log("Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender);
}
};
In the example of your question you have them corrected.
Double check the code you post, please.
After looking at your test in jsbin, I noticed that in your assert.deepEqual() method you run formLetter(letter) and compares it to the concatenated string you created.
The problem is that formLetter() expects three string values and you send it an object (letter). That's why you get [Object object] in the first location and undefined in the others.
You should run formLetter(letter.recipient, letter.msg, letter.sender) in your assert and it should work properly.
I want to test if the data array in the code below has content, because when a user gives a packageid (variable in the code below) that doesn't exist i want the else from the "if...else" to be executed. When i put in a packageid that exists everything works fine, but when i put in no number or a number that doesn't exist, the else side does't get evaluated.
function getInfoAndStatus(){
sym.$("customer_name").empty();
packageid = $("#tracknrfield").val();
var url = "http://student.howest.be/sylvain.vansteelandt/fedex/server/getPackageTracking.php?id=" + packageid;
$.getJSON(url,function(data){
if(data && data[0].id){
$("<span />", {
text: "Customer name: " + data[0].customer_name + " " + data[0].customer_firstname
}).appendTo(sym.$("customer_name"));
} else {
$("<span />", {
text: "The package with number " + packageid + " has not been found. Please try again."
}).appendTo(sym.$("customer_name"));
}
});
}
getInfoAndStatus();
Check your javacript console for any errors. data may be null or an empty array.
Adding a check for console.log(typeof data) may be useful as well.
Sight unseen, I'd most likely do something like if (data && data.length > 0)
If you check your console output, I'm betting you'll see an error there.
Your if check should look like:
if(data && data[0] && data[0].id)
As you may not have an element in your array.
I have a problem regarding this two values from my textboxes (text, text2), seems like text2 only show its value, is there any way I can get rid of these things. Any help would be so much appreciated.
parameters = ('text=' + document.getElementById('text').value) && ('text2=' + document.getElementById('text2').value;
xmlhttp.open('POST', 'try.php', true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(parameters);
parameters = ('text=' + document.getElementById('text').value) && ('text2=' + document.getElementById('text2').value;
This line is wrong. && does not mean "concatenate": it means "logical and". Basically, it means "return false if either side of the operator is falsy, otherwise, return the right-hand-side". So this will always return 'text2=' + document.getElementById('text2').value, because this is the right-hand-side of the && operator.
You need to concatenate them, for which the operator, as you use quite correctly otherwise, is +.
parameters = 'text=' + document.getElementById('text').value + '&' + 'text2=' + document.getElementById('text2').value;
Note that you should probably also use encodeURIComponent to make sure you're making valid HTTP requests:
parameters = 'text=' + encodeURIComponent(document.getElementById('text').value) + '&' + 'text2=' + encodeURIComponent(document.getElementById('text2').value);
Don't use && to concatenate strings, use +. There are also unbalanced brackets and you are missing the "&" to separate your parameters in the URL string.
parameters = 'text=' + document.getElementById('text').value + '&text2=' + document.getElementById('text2').value;