#node.js #mongodb #mongoose-schema
Вопрос:
Я застрял на вопросе, который сводит меня с ума. Я начал работать над проектом, который не разрабатывал сам, я здесь для того, чтобы обезопасить его, улучшить и так далее… Это первый раз, когда я работаю над Node.js и, Монго, я, возможно, пропустил некоторые концепции.
У меня есть эти две структуры:
var GameSchema = new BaseSchema();
GameSchema.add({
pseudo: String,
playable: {type: Schema.Types.ObjectId, ref: 'Playable', autopopulate: true},
investigation: {type: Schema.Types.ObjectId, ref: 'Investigation', autopopulate: true},
structure: {type: Schema.Types.ObjectId, ref: 'Structure', autopopulate: true},
startTime: Date,
endTime: Date,
finalTime: Number, //Final time in secs
nbHintsUsed: Number,
code: String,
disabled: Boolean,
ended: Boolean,
player: {type: Schema.Types.ObjectId, ref: 'Player', autopopulate: true},
uniqueDeviceId: String,
});
var GameUpdateSchema = new BaseSchema();
GameUpdateSchema.add({
game: {type: Schema.Types.ObjectId, ref: 'Game', autopopulate: true},
battery: Number,
position: String,
currentStep: Number,
progression: Number,
});
GameUpdate напрямую относится к GameUpdate.
Это работает при прямом запросе от Robo3T: Скриншот Robo3T
Следующий запрос не работает:
// Sort games by game id for this group (playable id)
Game.find({ "playable": idGroupGame})
.exec(function(err, games) {
if (err) { let message = 'Error on get game with playable ' idGroupGame ' : ' err.toString();return logErrorAndRes(res, 500, message); }
// Loop through games to get latest update of each
games.forEach(function(game) {
// Does not work from here
GameUpdate.find({ game: game })
.exec(function(err, gameUpdates) {
if (err) { let message = 'Error on get game update with id ' game._id ' : ' err.toString();return logErrorAndRes(res, 500, message); }
// Loop through game updates to keep
gameUpdates.forEach(function(gameUpdate) {
// Add this update to the list
let customUpdate = new CustomGameDetails.CustomGameDetailsUpdate(
//gameUpdate.game.pseudo,
"jr test",
"gameUpdate.progression",
"gameUpdate.lastUpdate",
"gameUpdate.battery",
"gameUpdate.currentStep",
"gameUpdate.position",
"id"
//gameUpdate.game._id
);
customUpdates.push(customUpdate);
});
});
// This does works
/*
let customUpdate = new CustomGameDetails.CustomGameDetailsUpdate(
//gameUpdate.game.pseudo,
"jr test2",
"gameUpdate.progression",
"gameUpdate.lastUpdate",
"gameUpdate.battery",
"gameUpdate.currentStep",
"gameUpdate.position",
game._id
//gameUpdate.game._id
);
customUpdates.push(customUpdate);
*/
});
// Build final answer
let customGamesDetails2 = new CustomGameDetails.CustomGameDetailsHeader(
"title","","",
null,
customUpdates);
res.json(customGamesDetails2);
});
Finding game documents by a reference with a playable document does work, i can see that by filling false data as you can see.
Direct reference is wotking on every other object i have worked on on this project BUT not on this one, it seems i cannot find game updates from a game reference.
Я также попытался использовать _id, новый объект с правильной ссылкой и т. Д… Я трижды проверил все, что смог найти на форумах и в документации, но вот я здесь, я понятия не имею, что здесь может произойти, и я не вижу, что я пропустил.
Пожалуйста, не указывайте, что какие-либо другие критерии работают в коде (по прогрессии, батарее…).
Любая помощь будет очень признательна, спасибо за ваше время!