Search code examples
javascriptnode.jshttp-status-code-404http-status-code-400deployd

Node.js Deployd 404 error on dpd.get & 400 error on dpd.put when using id parameter


Basically what the title says - I've been getting a 404 Not Found on all ids that I enter for "id":

dpd.items.get("id", function(results, error) {
 console.log(results);
});

And a 400 Bad Request on any value of "id":

dpd.items.put("id",{category:value},function(results, error){
 console.log("Record updated");
});

All the id values exist in the Deployd dashboard, and I am able to make get requests using any category parameters OTHER than ID.

Feels like I've tried everything at this point, please help!


Solution

  • This error can occur if you insert documents through a different client than deployd.

    From here:

    MongoDB uses ObjectIds as the default value for the _id field if the _id field is not specified ... if a client does not add an _id field, mongod will add an _id field that holds an ObjectId.

    Although the IDs created by mongoDB are visible in the deployd dashboard, they are not normal strings (like the IDs generated by deployd) and deployd does not find them when it's looking for a string.

    Try to run a query like the following with any other mongoDB client (e.g. Robomongo):

    db.yourcollection.find({_id: ObjectId("some_id_you_know_exists_in_collection")})
    

    If it does not throw an error, the id is most likely an ObjectId that was not created by deployd.

    Unfortunately, there is no easy fix. (At least not for big collections and complicated apps.) For SMALL collections I'd suggest to just duplicate the data into a new collection and let deployd create new IDs.

    Quick, dirty and untested:

    dpd.collection.get({}, function(res) {
        _.each(res, function(object){
            object.oldId = object.id //add id backup 
            delete object.id
    
            // post new object without id -> deployd creates a new id
            dpd.newcollection.post(object, function(res, err) {
                if(err) {
                    console.log(err);
                };
                if(res) {
                    console.log(res);
                };
            })
        });
    })
    

    You have to decide for yourself if that works for you.