Dans le schéma d’un modèle Mongoose, je dois exécuter une requête pour un autre modèle, mais ce deuxième modèle n’a pas encore été enregistré, ce qui entraîne l’erreur suivante. MissingSchemaError: Schema hasn't been registered for model "Vote".
Comment puis-je obtenir que cela fonctionne?
Voici comment j’amorce mes modèles dans server.js:
//Bootstrap models var models_path = __dirname + '/app/models'; var walk = function(path) { fs.readdirSync(path).forEach(function(file) { var newPath = path + '/' + file; var stat = fs.statSync(newPath); if (stat.isFile()) { if (/(.*)\.(js|coffee)/.test(file)) { require(newPath); } } else if (stat.isDirectory()) { walk(newPath); } }); }; walk(models_path);
Le modèle commence est appelé Object, qui, je crois, est enregistré avant Vote, ce qui pourrait être le problème:
// Object Schema // Module dependencies. var mongoose = require('mongoose'), Vote = mongoose.model('Vote'), Schema = mongoose.Schema; var ObjectSchema = new Schema({ short_id: { type: Ssortingng, required: true, unique: true }, created: { type: Date, default: Date.now } }); ObjectSchema.virtual('voted').get(function () { // Here is where I'd like to run a query with the Vote model and then set the virtual atsortingbute either true or false });