Thursday 8 June 2017

findOne mongo error in _id field [duplicate]

This question already has an answer here:

I maked just a casual blog post. Each post belongs to one category. I saved categogy id in posts collection and try to pick category data by _id that i picked from post collection. But each time i get error: "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters" . I dont know how to fix this. Thats my code:

router.get('/:url', function(req, res) {
  var posts = db.get('posts');
  // console.log('req.params.url = '+req.params.url);
  posts.findOne({"url":req.params.url},function(err,post) {
    if(err){
      console.log('ERROR findOne post!');
      console.log(err);
    }
    console.log('post.category = '+post.category); // post.category = 59388f89d672932a9f63485f
    var postCategories = db.get('categories');
    postCategories.findOne({id:post.category},function(err, postCat) {
      if(err){
        postCategories.find({},{} , function(err, cats) {
          cats.forEach(function(cat) {
            console.log('cats id = '+cat._id);
          });
          // cats id = 593743b24b63ef3f6d31c0b2
          // cats id = 59388f89d672932a9f63485f
          console.log('ERROR findOne postCat!');
          console.log(err);
        })
      } 



via Nikita M

No comments:

Post a Comment