모델에 대한 정보를 조정하려고합니다. 나는 model.find ({_ id : "id", function (err, foundModel) {}); 기능. 나 또한 findById 및 findByIdAndUpdate로 시도했지만 모두 작동하지 않는 것 같습니다.몽구스 findById 반환 null
문제는 찾기 기능이 결과를 반환하지 않으며 오류가 발생하지 않는다는 것입니다. 여기
는 모델 :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;
그리고 여기에 코드입니다 :
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
내가 :
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");
}
});
});
이 내가 조금 주위를 조정 한 후에 얻을 출력은 콘솔 로깅은 ID가 있다는 것과 객체/배열이 중첩되어 있음을 증명합니다.
문제는 Rider.find({_id: rider}, function(err, foundRider){
이 null
을 반환한다는 것입니다.
누구나 도움이 될 수 있습니까? 더 많은 정보가 필요하면 알려주십시오.
최저
,룻거
모델 파일을 어떻게 반입하고 있습니까? 나는 라우터 파일 (모델에서만)의 어디에서나 정의 된 '라이더 (Rider)'를 볼 수 없습니까? – wrangler
귀하의 회신을 위해 Hey Wrangler, Tnx. 게시물에 헤더를 추가했습니다. 나는 그것을 포스트에 포함시키지 않았지만, 이미 거기에 있었다. 그래서 불행하게도 문제는 아니다. – RutgerBrns