Search code examples
angularjsmongodbsails.jssails-mongo

mongodb ObjectId find with sails, returns all the data


in my sails controller I have implement a function to find users. but when I try to search using ObjectId it returns all the data. I couldn't find any reason. but it works when I create a new separate field as "userID", and find using "userID". I think the problem is find using the "ObjectId" what could be the issue.

module.exports = {

    updateOrders : function(req,res){

    var ObjectId = require('mongodb').ObjectID;
    console.log("req " + req);
    var data = req.body;
    var obj = [];


console.log("\ndata "+(data));

var jdata = JSON.stringify(data);
console.log("\njdata: "+jdata);

    data.forEach(function(orders){

        var objid = ObjectId(orders.id);
        console.log("Obj orders id  :" + objid);


        ApplicationOrder.find({id: ObjectId(orders.id)}).exec(function (err, order) {

            if (err){
                return done(err);
            }

            console.log('order ' + order);
             obj.push(order);
        });


    })
        res.send(obj);




        }


};

Json data: printed in the console.

{
    "_id" : ObjectId("59a8f820caf1cae37af72c0c"),
    "appId" : "59a669431954a69e37c20b69",
    "registeredUser" : "59a677811954a69e37c20b73",
    "item" : [ 
        {
            "id" : "59a669441954a69e37c20b70",
            "name" : "Short x",
            "qty" : 1,
            "sku" : "1111",
            "totWeight" : null,
            "price" : "250",
            "total" : "250",
            "imgURL" : [ 
                {
                    "img" : "short_x.png"
                }
            ],
            "totalQty" : 218
        }
    ],
    "amount" : "250",
    "customerName" : "dilakshan",
    "deliverName" : "dilakshan",
    "deliveryNo" : "11010",
    "deliveryStreet" : "delgoda",
    "deliveryCity" : "kandana",
    "deliveryCountry" : "Sri Lanka",
    "deliveryZip" : "11010",
    "telNumber" : 94779967409,
    "tax" : "0",
    "shippingCost" : "0",
    "shippingOpt" : "Flat Rate",
    "email" : "[email protected]",
    "currency" : "$",
    "paymentStatus" : "Pending",
    "fulfillmentStatus" : "Pending",
    "createdAt" : ISODate("2017-09-01T06:03:12.584Z"),
    "updatedAt" : ISODate("2017-09-01T06:03:12.584Z")
}
{
    "_id" : ObjectId("59a8f82fcaf1cae37af72c0d"),
    "appId" : "59a669431954a69e37c20b69",
    "registeredUser" : "59a677811954a69e37c20b73",
    "item" : [ 
        {
            "id" : "59a669441954a69e37c20b6d",
            "name" : "Shirt x",
            "qty" : 1,
            "sku" : "1111",
            "totWeight" : null,
            "price" : "250",
            "total" : "250",
            "imgURL" : [ 
                {
                    "img" : "shirt_x.png"
                }
            ],
            "totalQty" : 244
        }
    ],
    "amount" : "250",
    "customerName" : "dilakshan",
    "deliverName" : "dilakshan",
    "deliveryNo" : "11010",
    "deliveryStreet" : "delgoda",
    "deliveryCity" : "kandana",
    "deliveryCountry" : "Sri Lanka",
    "deliveryZip" : "11010",
    "telNumber" : 94779967409,
    "tax" : "0",
    "shippingCost" : "0",
    "shippingOpt" : "Flat Rate",
    "email" : "[email protected]",
    "currency" : "$",
    "paymentStatus" : "Pending",
    "fulfillmentStatus" : "Pending",
    "createdAt" : ISODate("2017-09-01T06:03:27.022Z"),
    "updatedAt" : ISODate("2017-09-01T06:03:27.022Z")
}
{
    "_id" : ObjectId("59a8f83ecaf1cae37af72c0e"),
    "appId" : "59a669431954a69e37c20b69",
    "registeredUser" : "59a677811954a69e37c20b73",
    "item" : [ 
        {
            "id" : "59a669441954a69e37c20b71",
            "name" : "Beach Suit",
            "qty" : 2,
            "sku" : "1111",
            "totWeight" : null,
            "price" : "250",
            "total" : "250",
            "imgURL" : [ 
                {
                    "img" : "cloth3.png"
                }
            ],
            "totalQty" : 238
        }
    ],
    "amount" : "500",
    "customerName" : "dilakshan",
    "deliverName" : "dilakshan",
    "deliveryNo" : "11010",
    "deliveryStreet" : "delgoda",
    "deliveryCity" : "kandana",
    "deliveryCountry" : "Sri Lanka",
    "deliveryZip" : "11010",
    "telNumber" : 94779967409,
    "tax" : "0",
    "shippingCost" : "0",
    "shippingOpt" : "Flat Rate",
    "email" : "[email protected]",
    "currency" : "$",
    "paymentStatus" : "Pending",
    "fulfillmentStatus" : "Pending",
    "createdAt" : ISODate("2017-09-01T06:03:42.439Z"),
    "updatedAt" : ISODate("2017-09-01T06:03:42.439Z")
}

Solution

  • I finally was able to fix the issue. I had to edit the my model class in sails and use only orders.id model class,

    _id:{type : 'objectid'} and when search

    .find({_id: orders.id})

    this worked for me perfectly.