Mongodb Objectid returning [Object Object] in Ajax

Asked

Viewed 83 times

1

In a query I do in Mongodb using Ajax and PHP, when I manipulate Objectid already in javascript, it returns me as [Object Object]. How do I use it as a string?

It follows code $.ajax()

$.ajax({
    url: 'funcoes/registroeventos.php',
    data: {
        "ref": ref
    },
    type: 'post',
    dataType: 'json',
    cache: false,
    beforeSend: function (xhr) {
    },
    error: function (jqXHR, textStatus, errorThrown) {
    },
    success: function (dados) {
        $.each(dados, function () {
            $.each(this, function (index, value) {
                alert(value);
            });
        });
    }
});

Follow the PHP code:

$ref = $_POST['ref'];
    try {
        $consulta = ['ref' => $ref, 'excluido' => 'n'];
        $opcoes = [];
        $query = new MongoDB\Driver\Query($consulta, $opcoes);
        $linhas = $conexao->executeQuery($bd . "maquinas", $query);

        echo json_encode(iterator_to_array($linhas));
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }

Example of Json:

[
     {"_id":{"$oid":"5aafac02dc32b7f93a3fda00"}, "ref":"DIP001", 
      "nome":"Dip Tork", "status":"setup", "excluido":"n",
       "idsetor":"5aafaba2dc32b7f93a3fd9ff"} 
]
  • Take the example of Javascript

  • 1

    You mean the json? [{"_id":{"$oid":"5aafac02dc32b7f93a3fda00"}, "ref":"DIP001", "name":"Dip Tork", "status":"setup", "excluded":"n", "idsector":"5aafaba2dc32b7f93a3fd9ff"} ]

  • That you can’t get the value?

  • I tried to access it in several ways. But it always returns me as [Object Object]. All other values it returns normal. But the Objectid always as Object.

  • I made an example on that json of the commentary !!!

1 answer

0


From your comment, you realize you’ve had a hard time understanding, this JSON is actually a array information, ie has positions for each item, example:

let item = [{"_id":{"$oid":"5aafac02dc32b7f93a3fda00"}, 
             "ref":"DIP001", "nome":"Dip Tork", 
              "status":"setup", "excluido":"n",
               "idsetor":"5aafaba2dc32b7f93a3fd9ff"} ] ;

console.log(item[0]['_id']['$oid']);
console.log(item[0]['ref']);
console.log(item[0]['nome']);
console.log(item[0]['status']);
console.log(item[0]['excluido']);
console.log(item[0]['idsetor']);

in this case, there is only one item in the array which is the 0, but if it contains more you have to check the quantity of items in the array to search your items, example:

let item = [{
  "_id": {
    "$oid": "5aafac02dc32b7f93a3fda00"
  },
  "ref": "DIP001",
  "nome": "Dip Tork",
  "status": "setup",
  "excluido": "n",
  "idsetor": "5aafaba2dc32b7f93a3fd9ff"
}, {
  "_id": {
    "$oid": "5aafac02dc32b7f93a3fdaaa"
  },
  "ref": "DIP001",
  "nome": "Dip Tork",
  "status": "setup",
  "excluido": "n",
  "idsetor": "5aafaba2dc32b7f93a3fd9ff"
}];
console.log('Quantidade de itens:' + item.length);
for (i = 0; i < item.length; i++) {
  console.log('-----------------------------');
  console.log(item[i]['_id']['$oid']);
  console.log(item[i]['ref']);
  console.log(item[i]['nome']);
  console.log(item[i]['status']);
  console.log(item[i]['excluido']);
  console.log(item[i]['idsetor']);  
}

  • 1

    I was really missing the right reference. I was trying to Alert(data. _id); instead of Alert(data. _id. $oid);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.