This works when we do this:
$db = $connection->messages;
$collection = $db->messagesCollection;
$messageArray = $collection->find(array('IDto' => '4'));
foreach($messageArray as $messageData){
$messageFrom = $messageData['IDfrom'];
$messageTo = $messageData['IDto'];
$messageTitle = $messageData['messageTitle'];
$messageIfRead = $messageData['ifRead'];
}
$JSONData = array('true', $messageFrom, $messageTo, $messageTitle, $messageIfRead);
echo $_GET['callback']."(".json_encode($JSONData).")";
But when we do this:
$db = $connection->messages;
$collection = $db->messagesCollection;
$messageArray = $collection->find(array('IDto' => '4'));
$JSONData = array('true', $messageArray);
echo $_GET['callback']."(".json_encode($JSONData).")";
and in the Javascript do this:
$.getJSON("mySite.com/pullData/getMail.php?callback=?",{
request: requestVar},
function(recievedData) {
alert(recievedData);
})
We get an alert of true, [object Object]
When using console log we get Object {}
How do we send that table data correctly?
I believe your biggest problem is the
MongoCursor:You are trying to encode an object of
MongoCursorthere, hence the string representation is[object Object].Try:
Instead. All
findfunctions in MongoDB return aMongoCursor, the reason why your first code works is because you iterate the cursor building up your objects:Note as well that the default
json_encodeof a document will contain objects, such asMongoIdandMongoDatethat do not encode too well into reusable JSON syntax. As such you will need to handle these types yourself.Edit
Maybe a better way is to actually redo the indexes manually:
This way you will have a
0based incrementing index instead of the ObjectId as each index base.