简体   繁体   中英

Undefined when finding Embedded Objects in Mongoose

I'm using AngularJs, Node.js, MongoDB & Express and what I'm trying to do is to get a list of Projects that a client has , count them and return that count. The code is in spanish but it's pretty much the same thing as english.

my code goes as follows:

proyectosAsignadosAlCliente: function(idCliente,callback){
    dbConn.conectar(function(db){
        db.collection("proyectos")
        .find({"cliente._id":  new ObjectId(idCliente)}, function(err,cantidad){
            if(err) throw err;
            if(cantidad){
                callback(cantidad.toArray().length);
            }
            else{
                console.log("Entro al else de undefined");
                cantidad = 0;
                callback(cantidad);
            }
        });

My main problem is that I'm allways getting "undefined"

I really think that my problem is Here:

.find({"cliente._id":  new ObjectId(idCliente)}

I don't have any Mongoose Schema and I don't know if "cliente._id" is the right way to do I'm lost. Thanks in Advance you guys are the best!

If I use findOne it goes to the "else" and returns me cantidad = 0; but since I want more than "one" project I'm using find that always returns undefined but It is not going through the "else"

UPDATE

When I go to the DB:

>use TP
>show collections
clientes
proyectos
>db.clientes.find()
{ "_id" : ObjectId("57f6a1be9a96ad239cb03ccc"), "nombre" : "cliente1", "correo" : "cliente1@correo.com" }
>db.proyectos.find()
{ "_id" : ObjectId("57f707d348e52c0e381bd4ca"), "nombre" : "123", "cliente" : { "_id" : "57f6a1be9a96ad239cb03ccc", "nombre" : "cliente1", "correo" : "cliente1@correo.com" }, "fechaInicio" : "1412-03-12T03:00:00.000Z", "presupuesto" : 2, "duracion" : 23123 }

I've tried in mongodb's shell with:

> db.proyectos.find({'clientes._id': '57f6a1be9a96ad239cb03ccc'})

or

> db.proyectos.find({"clientes._id": ObjectId("57f6a1be9a96ad239cb03ccc") } )

and the shell doesn't return anything, same if I go with "cliente" or "clientes"

Please:

  • Make sure you can execute the find in mongo shell or your favorite editor
  • Make sure you have the following line:

    var ObjectId = require('mongoose').Types.ObjectId;

for this line to work:

.find({"cliente._id":  new ObjectId(idCliente)}, function(err,cantidad){

When the find in shell / editor is working and not in your code please add a sample entry from your db

Edit After using your DB items i notice your embeded cliente document contains a _id field which is NOT a ObjectId. It's a ObjectId stored as a string. Use the query below.

.find({ "cliente._id" : "57f6a1be9a96ad239cb03ccc"})

I do advise you to make sure the value you store is a ObjectId type and not a ObjectId stored as string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM