制作一个投票应用程序,其中包含以下2个模型用户和民意调查以及包含两个集合用户和民意调查的数据库。
user.js的
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = new Schema({
github: {
id: String,
displayName: String,
username: String,
publicRepos: Number
},
nbrClicks: {
clicks: Number
}
});
module.exports = mongoose.model('User', User);
Poll.js
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Poll = new mongoose.Schema({
title: { type: String, required: true },
creator: String,
choices: [String],
votes: [Number]
});
module.exports = mongoose.model('Poll', Poll);
这是路线部分
'use strict';
require('dotenv').load();
var path = process.cwd();
var ClickHandler = require(path + '/app/controllers/clickHandler.server.js');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.createConnection(process.env.MONGO_URI);
module.exports = function (app, passport) {
function isLoggedIn (req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
res.redirect('/login');
}
}
var clickHandler = new ClickHandler();
app.route('/').get(isLoggedIn, function (req, res) {
//console.log(display-name.profile-value);
res.render(path + '/public/index.jade');
});
app.route('/polls').get(function(req, res) {
var polls = db.get('polls');
console.log(polls);
res.render(path + '/public/polls.jade');
})
这是抛出的错误
TypeError: db.get is not a function
at /home/ubuntu/workspace/app/routes/index.js:39:22
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/workspace/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
at SessionStrategy.strategy.pass (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:325:9)
at SessionStrategy.authenticate (/home/ubuntu/workspace/node_modules/passport/lib/strategies/session.js:71:10)
at attempt (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/home/ubuntu/workspace/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:312:13)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:271:10)
at initialize (/home/ubuntu/workspace/node_modules/passport/lib/middleware/initialize.js:53:5)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:312:13)
at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/ubuntu/workspace/node_modules/express/lib/router/index.js:330:12)
答案 0 :(得分:2)
首先,您需要将轮询导入路径文件。
var Poll = require('./PATH_TO_POLL/Poll');
然后正如@ numbers1311407所述,如果你想要返回所有已保存的民意调查文件,请尝试:
Poll.find(function (err, polls) {
if (err) return console.error(err);
console.log(polls);
})
修改强>
有关module.exports的更多信息,请访问here