2017-12-10 11 views
0

モデルに関する情報を調整しようとしています。私はこれをmodel.find({_ id: "id"、function(err、foundModel){})を使ってやっています。関数。私もfindByIdとfindByIdAndUpdateで試してみましたが、うまくいけません。Mongoose findById nullを返す

問題は、find関数が結果を返さないことと、エラーを返さないことです。ここで

はモデルです:

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を返すという問題があります。

誰でも私にこれを手伝うことができますか?もっと情報が必要な場合はお知らせください。

ベスト、

ルトガー

+0

あなたのモデルファイルをどのようにインポートしていますか?あなたのルータファイルのどこにでも定義されている 'Rider'を見ることはできません(モデルのみ)? – wrangler

+0

あなたの返事のためにちょっとラングラー、Tnx。投稿にヘッダーを追加しました。私はポストにそれを含めなかったが、それはすでに残っていたので、残念ながら問題ではなかった。 – RutgerBrns

答えて

0

OMG。私はついに何も働いていないことを知りました。私はサーバーを起動するたびに播種しています。私のライダーは常に完全に新しいIDで取り除かれ、再作成されます。結果に保存されている古いものは決して動作しません。

ol。

関連する問題