制作一个投票应用程序,包括以下两个模型用户和投票和数据库组成的两个集合用户和投票。
用户.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);

投票.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)

最佳答案

首先需要将轮询导入到路由文件中。

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

关于node.js - 显示'db.get不是函数'NodeJs,MongoDB,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38701123/

10-09 04:42