load : function() {
var mastery = $('div.mastery.trees');
this.object = $.parseJSON($('a.json.data', mastery).text());
console.log(this.object);
}
this fails silently (unless i add a try/catch block around parseJSON) regardless of the content of the json data (the data i want to use - see below - or even just some very simple test data)
actual data
{"points":{"allowed":30,"current":0,"offense":0,"defense":0,"utility":0},"current":{"offense":{},"defense":{},"utility":{}},"trees":{"offense":{"Deadliness":{"max":3,"req":""},"Cripple":{"max":1,"req":""},"Plentiful_Bounty":{"max":1,"req":""},"Archmages_Savvy":{"max":3,"req":""},"Sorcery":{"max":4,"req":{"offense":4}},"Alacrity":{"max":4,"req":{"offense":4}},"Burning_Embers":{"max":1,"req":{"offense":8}},"Archaic_Knowledge":{"max":1,"req":{"offense":8,"Sorcery":4}},"Sunder":{"max":3,"req":{"offense":8}},"Offensive_Mastery":{"max":2,"req":{"offense":8}},"Brute_Force":{"max":3,"req":{"offense":12}},"Lethality":{"max":3,"req":{"offense":16}},"Improved_Rally":{"max":1,"req":{"offense":16}},"Havoc":{"max":1,"req":{"offense":20}}},"defense":{"Menders_Faith":{"max":1,"req":""},"Resistance":{"max":3,"req":""},"Preservation":{"max":1,"req":""},"Hardiness":{"max":3,"req":""},"Strength_of_Spirit":{"max":3,"req":{"defense":4,"Resistance":3}},"Evasion":{"max":4,"req":{"defense":4}},"Defensive_Mastery":{"max":2,"req":{"defense":8}},"Nimbleness":{"max":1,"req":{"defense":8,"Evasion":4}},"Harden_Skin":{"max":3,"req":{"defense":8,"Hardiness":3}},"Veterans_Scars":{"max":4,"req":{"defense":12}},"Willpower":{"max":1,"req":{"defense":12}},"Ardor":{"max":3,"req":{"defense":16}},"Reinforce":{"max":1,"req":{"defense":16}},"Tenacity":{"max":1,"req":{"defense":20}}},"utility":{"Spatial_Accuracy":{"max":1,"req":""},"Good_Hands":{"max":3,"req":""},"Perseverance":{"max":3,"req":""},"Haste":{"max":1,"req":""},"Awareness":{"max":4,"req":{"utility":4}},"Expanded_Mind":{"max":4,"req":{"utility":4}},"Greed":{"max":1,"req":{"utility":8}},"Meditation":{"max":3,"req":{"utility":8}},"Utility_Mastery":{"max":2,"req":{"utility":8}},"Insight":{"max":1,"req":{"utility":8}},"Quickness":{"max":3,"req":{"utility":12}},"Blink_of_an_Eye":{"max":1,"req":{"utility":12}},"Intelligence":{"max":3,"req":{"utility":16}},"Mystical_Vision":{"max":1,"req":{"utility":16}},"Presence_of_the_Master":{"max":1,"req":{"utility":20}}}}}
test data
{"json":1}
jsonlint.com tells me both of these data-sets are valid json so why wont it parse properly?
all the data comes from a php array thats converted via json_encode()
return array(
"points" => array( // for js
"allowed" => 30,
"current" => 0,
"offense" => 0,
"defense" => 0,
"utility" => 0,
),
"current" => array( // for js
"offense" => new stdClass,
"defense" => new stdClass,
"utility" => new stdClass,
),
"trees" => array(
"offense" => $offense, // the trees
"defense" => $defense,
"utility" => $utility,
),
);
been staring at it for a while and just cant see where the error would be coming from
does anyone have any ideas?
From your question:
a simple mistake being overlooked that i only picked up on when
checking the console.log as suggested by JamWaffles
the gist of it is this; var mastery is picking up 2 divs (the parent
that i want and the direct child which i dont) - and as both are
parents to the json data.. thats where the duplication comes from
silly
thanks jamwaffles!
Related
I'm trying to pass data about the page state (navbar links having active class when you are in that exact page), page title. I do so with an indexed array $pageInfo, however I am getting a syntax error and doen't know where?
Also do you think this is a good method or should I use view->share() instead?
public function clases()
{
$pageInfo[] =
(
'page_title' => 'Clases',
'menu_active' => 'CLases',
'sub_menu_active' => '',
);
return view('clases.list', compact('pageInfo'));
}
public function domicilio()
{
$pageInfo[] =
(
'page_title' => 'Clases a domicilio',
'menu_active' => 'Clases',
'sub_menu_active' => 'Clases a domicilio',
);
return view('clases.domicilio', compact('pageInfo'));
I suggest you read PHP basic syntax.
Basically you want to do this:
$pageInfo =
[
'page_title' => 'Clases',
'menu_active' => 'CLases',
'sub_menu_active' => '',
];
Arrays have a syntax of [key => val, ...] in PHP, you're using () as it seems.
Also $someArray[] = someValue, will append the someValue to an existing array, in your case that would create another, unwanted level of your array.
And last, you're not ending the domicilio() function. But I'll assume you just didn't paste it in (you should add } at the end, if that's not the case).
I've many parameters(filters) for getting data from API so i've used POST method for sending parameters(filters) to API . Its working fine but I want to know its ok to use POST function for getting list from API ?
Reason for using POST instead GET :
If I've used GET function it will create problem for array data and also We need to pass some string data that may contain some special characters so in GET method need lot off manual error handling.
Our application under development phase so when new parameter need to add every time we need to change the URL on all our applications where we use this API.
$filter = (object) array(
'page_name' => '',
'page_category' => 0,
'page_gender' => 'N',
'page_country' => 0,
'page_company' => 0,
'page_language' => 0,
'page_order' => 'ASC',
'page_order_field' => 'dlang_firstname',
'page_currency' => 47,
'page_currency_unit' => 1,
'page_currency_iso' => 'GBP',
'page_current' => $page,
'page_size' => env('PAGE_SIZE', 10),
);
Above data I've passing to API.Is there any other way to send data ?
First, I want to say that this is more of a PHP Mongo Driver issue than MongoDB issue.
I have a problem with calling MapReduce through PHP. I have 3 custom functions inside Mongo for MapReduce: mapItems, reduceItems, finalizeItems.
When I test my functions from inside Mongo Shell, everything works great:
db.loadServerScripts();
db.runCommand({
mapreduce: 'items',
map: mapItems,
reduce: reduceItems,
finalize: finalizeItems,
out: {inline: 1},
scope: {members: {a1: 0, a2: 0}
});
Now when I try to do the same in PHP Mongo Driver, nothing works.
$db->command([
'mapreduce' => 'items',
'map' => 'mapItems',
'reduce' => 'reduceItems',
'finalize' => 'finalizeItems',
'out' => ['inline' => 1],
'scope' => ['members' => ['a1' => 0, 'a2' => 0]]
]);
I thought maybe I need to use MongoCode for the functions, but that still didn't work:
$db->command([
'mapreduce' => 'items',
'map' => new \MongoCode('mapItems'),
'reduce' => new \MongoCode('reduceItems'),
'finalize' => new \MongoCode('finalizeItems'),
'out' => ['inline' => 1],
'scope' => ['members' => ['a1' => 0, 'a2' => 0]]
]);
Then I thought maybe the PHP driver is being stupid, so I copy pasted the functions into PHP and tried them:
$db->command([
'mapreduce' => 'items',
'map' => new \MongoCode($mapFn),
'reduce' => new \MongoCode($reduceFn),
'finalize' => new \MongoCode($finalizeFn),
'out' => ['inline' => 1],
'scope' => ['members' => ['a1' => 0, 'a2' => 0]]
]);
And this worked.
How do I need to pass the internal functions to Mongo through PHP?
What you have done so far
This is a classic case of things not working how you seem to think they work. Let's break this down logically for everyone to understand.
So what you clearly have done to date is create some JavaScript function definitions and stored them in system.js. Now anything that has been stored in such a way can be called by server side JavaScript operations, which is fine. An example:
db.system.js.save({
"_id": squareThis",
"value": function(x) { return x*x; }
})
db.test.insert({ "a": 3 ))
db.test.find(function() { return squareThis(this.a) == 9 })
So that works as expected where the "server" can use the function. Not if you did this ( as you mention earlier ) :
db.loadServerScripts();
Now any functions in system.js are now available for the "shell client" to use as well. So you can now do this in the JavaScript REPL:
> squareThis(3) == 9
true
What you are doing wrong
Now you want to code with PHP. So you go to call your JavaScript methods you created on the "server" from your PHP code and you try this:
'map' => new \MongoCode('mapItems'),
So what is MongoCode? Well it basically defines a BSON type for a code reference ( expected to be JavaScript ) and sends that to the server.
The clear problem here is that "mapItems" is not JavaScript code. You "shell client" recognized that, but only after calling db.loadServerScripts(), which of course it can do, because that client understands JavaScript.
The same is true for the earlier attempt. Your PHP mapReduce command expects to have MongoCode objects that actually contain valid JavaScript. These are going to be sent to the server where it can acutally call functions defined there, but you still need to provide the mapReduce command with "real code" that does something.
Fixing It
Fortunately there is a way around this, in that you can basically "define a function to call a function" essentially "wrapping" the method calls so the "server" loaded code can be called.
The sytax varies depending on if the function is supposed to return anything so:
$db->command([
'mapreduce' => 'items',
'map' => new \MongoCode('function() { mapItems(this) }'),
'reduce' => new \MongoCode('function(key,values) { return reduceItems(key,values) }'),
'finalize' => new \MongoCode('function(key,value) { return finalizeItems(key,value)}'),
'out' => ['inline' => 1],
'scope' => ['members' => ['a1' => 0, 'a2' => 0]]
]);
So now your PHP mapReduce command sends up it's "packaged command" with valid JavaScript functions attached to the appropriate method stages. These "code wrappers" execute on the "server" and can then call the respective "server" methods that are stored in system.js.
The reason for the explicit argument passing i.e
function(key,values){ serverFunction(key,values) }
Is because your "wrappers" need to create the same sort of "signatures" as expected by the functions of "mapReduce" in general. And since they are "calling" the other methods "server side", then you need to "pass-through" the expected parameters.
This also has another implication for the "mapper" function. You are likely referring to all document content through the this context of JavaScript objects. Once you do this then you cannot do that anymore and will need to "explicitly" define a document argument to your "mapper"
db.system.js({
"_id": "mapper",
"value": function(doc) {
// use doc instead of this
}
And then even from the Mongo Shell you will also need to call like this:
db.runCommand({
"mapReduce": "items",
"mapper": function() { mapper(this) }
In order for the logic to work.
So server side functions are not really what you thought they are. You can use them, but you need to follow the rules of how they can be used.
In general it is probably better that you simply just code all of these up in your client rather than jump through all the hoops. It is afterall "JavaScript" execution for which the only language is "JavaScript", so that is why alternate methods of processing are generally preferred as long as they can be applied.
And if your document structure needs JavaScript processing, then you probably have your document structure wrong in the first place. So the better case here is "re-evaluate" your structure and processing needs to get you "cleaner" and "faster" code.
My app uses CakePHP (2.2.5) to get data from a SOAP server. I put logging into the SoapSource.php connector to see the XML returned and to display the array returned:
$result = $this->client->__soapCall($method, array('parameters' => $tParams));
Logger::write('SOAP Last Response', $this->client->__getLastResponse(), 3, 'transaction');
Logger::write('SOAP Last Response Object', print_r($result, TRUE), 3, 'transaction');
But what I'm seeing in the log is that two (recently-added) fields present in the XML are missing from the array, specifically, the last two before the RPList (formatted here but otherwise verbatim):
<transactionResult>
<id>test</id>
<resultCode>0</resultCode>
<ReadScheduledRecordingsRsp>
<RecordingDefinitionList>
<RecordingDefinition>
<RDId>d8c16d8f-67c6-469a-83c3-d51d8f8859a9</RDId>
<Title>The Young and the Restless</Title>
<SeriesId>4422</SeriesId>
<KeepUntil>SpaceIsNeeded</KeepUntil>
<StartPadSeconds>0</StartPadSeconds>
<EndPadSeconds>0</EndPadSeconds>
<Frequency>EveryDay</Frequency>
<KeepAtMost>0</KeepAtMost>
<Priority>23</Priority>
<ShowType>Any</ShowType>
<AirtimeDomain>SpecificTime</AirtimeDomain>
<ChannelDomain>SpecificChannel</ChannelDomain>
<RPList>
...
Followed by:
ARRAY
(
[transactionResult] => stdClass Object
(
[id] => test
[resultCode] => 0
[ReadScheduledRecordingsRsp] => stdClass Object
(
[RecordingDefinitionList] => stdClass Object
(
[RecordingDefinition] => Array
(
[0] => stdClass Object
(
[RDId] => d8c16d8f-67c6-469a-83c3-d51d8f8859a9
[Title] => The Young and the Restless
[SeriesId] => 4422
[KeepUntil] => SpaceIsNeeded
[StartPadSeconds] => 0
[EndPadSeconds] => 0
[Frequency] => EveryDay
[KeepAtMost] => 0
[Priority] => 23
[ShowType] => Any
[RPList] => stdClass Object
(
...
I'm guessing I just forgot or didn't know to do something, but I don't know what. It's just very suspicious that the two fields that don't work are the two that were just added, but I can't find any place where the fields are enumerated.
The WSDL doesn't specify any fields at all, just that there will be a bunch of data inside a transactionResult.
In the connect call, the options likewise don't specify any fields.
It all worked fine until the back-end added those two fields, and it all still works fine, except that I can't see the 2 new fields in the object.
Any ideas?
It would be nice to see the XML for RPList. What version of PHP/libxml?
Have you actually validated the XML returned by __getLastResponse()?
It sounds to me like your using WSDL mode for the client but your WSDL file lacks precision. Try using a WSDL which actually describes the data being served up by the API.
Am I understanding that the $result object when dumped lists RPList but the value is not what you expect? If so you might be able to get away with adding the SOAP_SINGLE_ELEMENT_ARRAYS option to the clients constructor: new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));. That's a long shot though. Need to see WSDL and full output from __getLastResponse().
Any errors? Try wrapping the call like so:
try {
$client = new SoapClient ('http://yoursite.com?WSDL',
array("trace" => 1,
"exceptions" => true,
"cache_wsdl" => WSDL_CACHE_NONE,
"features" => SOAP_SINGLE_ELEMENT_ARRAYS)
);
$result = $client->SomeMethod($data);
} catch (SoapFault $exception) {
var_dump($exception);
} catch (Exception $exception) {
var_dump($exception);
}
Short of fixing the WSDL/XML response your workaround seems like the best solution.
I am trying to use a WSDL web service with php/soap. And it always gives a deserializing error which is exactly:
Error in deserializing body of request message for operation 'Test'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'Test' and namespace ''. Found node type 'Element' with name 'parameters' and 'namespace'
When I test the WSDL source with WcfTestClient software nothing goes wrong and it returns with desired results.
When I compare the request XML which created by SOAP and XML which created by WcfTestClient software, I see that difference may be the problem. It seem like something wrong with namespace and prefixes, but I don't know how to solve it, or maybe it is something else causes the problem.
The request XMLs is this: http://pastebin.com/eysnG89F.
In case you need PHP code, this is the code I am using.
try{
$soap_options = array(
'soap_version' => SOAP_1_1,
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => TRUE
);
$soap = new SoapClient(
'http://url.to/web/service.svc?wsdl', $soap_options
);
$a = $soap->Test(
array("login" =>
array(
"FirmaId" => 15,
"KullaniciAdi" => "Asdf",
"Parola" => "Xyxy",
)
)
);
var_dump($a);
} catch (Exception $e) {
var_dump($e);
}
I cant find the exact cause and solution but this seems like working.
First, when you give object instead of array for the wsdl method, you can get rid of item, value nodes. Node will be created by object name
$a = $soap->Test(
(object)array("login" =>
(object)array(
"FirmaId" => 15,
"KullaniciAdi" => "Asdf",
"Parola" => "Xyxy",
)
)
);
After that i realized an < parameters> node instead of < WsdlMethod> node created in request xml. With overriding __doRequest method and replacing strings, can get rid this problem but still cant see the response. Need to wrap < wsdlMethodResponse> node with < parameter> via str_replace and finally get desired response.
As i said this may be a temporary solution. But working for me :)