0
我有問題,使我的server.js使用我的模型。Nodejs包括模型
我確實有其他機型相同的結構,例如像:
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
,但他們工作得很好..莫名其妙。
但是,如果,如果我嘗試使用:
var Crime = mongoose.model('Crime');
我得到:
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Crime".
如果我切換到:
var Crime = require('./models/Crime');
或
var Crime = require('./models/Crime.js');
我得到的迴應:
Cannot find module './models/Crime'
犯罪型號代碼:
var mongoose = require('mongoose');
var CrimeSchema = new mongoose.Schema({
username: {type: String, lowercase: true, unique: true},
time: String,
salt: String
});
CrimeSchema.methods.performCrime = function(pass) {
/*var deferred = $q.defer();
deferred.notify('loading....');
deferred.reject('Greeting is not allowed.');
return deferred.promise;
*/
return 22;
};
mongoose.model('Crime', CrimeSchema);
編輯
例如工作模式:
var Post = mongoose.model('Post');
和post.js模型
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
PostSchema.methods.upvote = function(cb) {
this.upvotes += 1;
this.save(cb);
};
PostSchema.methods.downvote = function (cb) {
this.upvotes -= 1;
this.save(cb);
};
mongoose.model('Post', PostSchema);
文件結束或其中o – maria
文件結束是。替換此代碼'mongoose.model('Crime',CrimeSchema);'我的回答 – John
我替換它,並且它得到了同樣的錯誤。 – maria