Mongoose findById返回null

时间:2017-12-10 16:37:51

标签: node.js mongoose

我正在尝试调整有关模型的信息。我这样做是通过使用model.find({_ id:“id”,function(err,foundModel){});功能。我也试过findById和findByIdAndUpdate,但似乎都不行。

问题是find-function不会返回任何结果,也不会产生错误。

以下是模型:

var mongoose = require("mongoose");

//CREATE THE SCHEMA
var riderSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    price: Number,
    points: {type: Number, default: 0},
    nationality: String,
    team: String,
    photo: String,
});

//CREATE THE MODEL
var Rider = mongoose.model("Rider", riderSchema);

//EXPORT THE MODEL
module.exports = Rider;

以下是代码:

var express = require("express");
var router = express.Router({mergeParams: true});
var passport = require("passport");
var User = require("../models/user");
var Race = require("../models/race");
var Result = require("../models/result");
var Rider = require("../models/rider");
var mongoose = require("mongoose");

var pointsArray = [
    250, 200, 175, 150, 130, 115, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5
    ];

router.get("/:id/finalize", function(req, res){
        Result.findById(req.params.id, function(err, foundResult){
            if(err){
                console.log("Something went wrong with finding the result to finalize");
            } else {
                console.log("This is the result I found: " + foundResult);
                var riders = [];
                riders = foundResult.riders;
                console.log(riders);

                var j = 0;

                riders.forEach(function(rider){
                    Rider.find({_id: rider}, function(err, foundRider){
                        if(err){
                            console.log("program tried to look up rider for the forEach loop finalizing the results, but could not find");
                        } else {
                            console.log(rider);
                            j = j + 1;
                            console.log("This is the " + j + "th rider I found: " + foundRider);
                            foundRider.points = foundRider.points + pointsArray[j];
                            console.log("done");
                        }
                    });
                });
                res.redirect("/results/admin");
            }
        });
    });

这是我稍微调整后得到的输出:

    This is the result I found: { _id: 5a2d259ab753ad08fc47881d,
  processed: 'results are filled in!',
  raceName: ' Tirreno-Adriatico (SR)',
  raceType: ' Stage Race',
  __v: 1,
  dateProcessed: 2017-12-10T12:17:35.243Z,
  riders: 
   [ 5a2d23cbb753ad08fc478809,
     5a2d23cbb753ad08fc47880a,
     5a2d23cbb753ad08fc47880b,
     5a2d23cbb753ad08fc47880c,
     5a2d23cbb753ad08fc47880d,
     5a2d23cbb753ad08fc47880e,
     5a2d23cbb753ad08fc47880f,
     5a2d23cbb753ad08fc478810,
     5a2d23cbb753ad08fc478811,
     5a2d23cbb753ad08fc478812,
     5a2d23cbb753ad08fc478815,
     5a2d23cbb753ad08fc478813,
     5a2d23cbb753ad08fc478814,
     5a2d23cbb753ad08fc478817,
     5a2d23cbb753ad08fc478816,
     5a2d23cbb753ad08fc478809,
     5a2d23cbb753ad08fc47880a,
     5a2d23cbb753ad08fc47880b,
     5a2d23cbb753ad08fc47880c,
     5a2d23cbb753ad08fc47880d,
     5a2d23cbb753ad08fc47880e,
     5a2d23cbb753ad08fc47880f,
     5a2d23cbb753ad08fc478810,
     5a2d23cbb753ad08fc478811,
     5a2d23cbb753ad08fc478812,
     5a2d23cbb753ad08fc478815 ],
  dateCreated: 2017-12-10T12:08:43.442Z }
["5a2d23cbb753ad08fc478809","5a2d23cbb753ad08fc47880a","5a2d23cbb753ad08fc47880b","5a2d23cbb753ad08fc47880c","5a2d23cbb753ad08fc47880d","5a2d23cbb753ad08fc47880e","5a2d23cbb753ad08fc47880f","5a2d23cbb753ad08fc478810","5a2d23cbb753ad08fc478811","5a2d23cbb753ad08fc478812","5a2d23cbb753ad08fc478815","5a2d23cbb753ad08fc478813","5a2d23cbb753ad08fc478814","5a2d23cbb753ad08fc478817","5a2d23cbb753ad08fc478816","5a2d23cbb753ad08fc478809","5a2d23cbb753ad08fc47880a","5a2d23cbb753ad08fc47880b","5a2d23cbb753ad08fc47880c","5a2d23cbb753ad08fc47880d","5a2d23cbb753ad08fc47880e","5a2d23cbb753ad08fc47880f","5a2d23cbb753ad08fc478810","5a2d23cbb753ad08fc478811","5a2d23cbb753ad08fc478812","5a2d23cbb753ad08fc478815"]
5a2d23cbb753ad08fc47880a
This is the 1th rider I found: 
done
5a2d23cbb753ad08fc478812
This is the 2th rider I found: 
done
5a2d23cbb753ad08fc478816
This is the 3th rider I found: 
done
5a2d23cbb753ad08fc47880d
This is the 4th rider I found: 
done
5a2d23cbb753ad08fc478812
This is the 5th rider I found: 
done
5a2d23cbb753ad08fc47880b
This is the 6th rider I found: 
done
5a2d23cbb753ad08fc47880f
This is the 7th rider I found: 
done
5a2d23cbb753ad08fc478813
This is the 8th rider I found: 
done
5a2d23cbb753ad08fc47880a
This is the 9th rider I found: 
done
5a2d23cbb753ad08fc47880c
This is the 10th rider I found: 
done
5a2d23cbb753ad08fc478811
This is the 11th rider I found: 
done
5a2d23cbb753ad08fc478817
This is the 12th rider I found: 
done
5a2d23cbb753ad08fc47880c
This is the 13th rider I found: 
done
5a2d23cbb753ad08fc47880d
This is the 14th rider I found: 
done
5a2d23cbb753ad08fc478810
This is the 15th rider I found: 
done
5a2d23cbb753ad08fc478814
This is the 16th rider I found: 
done
5a2d23cbb753ad08fc47880b
This is the 17th rider I found: 
done
5a2d23cbb753ad08fc478809
This is the 18th rider I found: 
done
5a2d23cbb753ad08fc47880e
This is the 19th rider I found: 
done
5a2d23cbb753ad08fc478815
This is the 20th rider I found: 
done
5a2d23cbb753ad08fc478809
This is the 21th rider I found: 
done
5a2d23cbb753ad08fc47880e
This is the 22th rider I found: 
done
5a2d23cbb753ad08fc47880f
This is the 23th rider I found: 
done
5a2d23cbb753ad08fc478811
This is the 24th rider I found: 
done
5a2d23cbb753ad08fc478810
This is the 25th rider I found: 
done
5a2d23cbb753ad08fc478815
This is the 26th rider I found: 
done

我控制台记录很多只是为了向自己证明ID在那里以及对象/数组是如何嵌套的。

问题在于Rider.find({_id: rider}, function(err, foundRider){会返回null

任何人都可以帮我这个吗?如果需要更多信息,请告诉我。

最佳,

拉​​特格

1 个答案:

答案 0 :(得分:0)

OMG。我终于找到了为什么没有什么工作。我每次启动服务器时都在播种。我的所有骑手都经常被移除并使用全新的ID重新创建。因此,存储在结果中的旧元素永远不会起作用。

洛尔。