我收到了ReferenceError:未定义mongoose。我是节点的新手,请在下面的App.js代码如下所示。我无法通过谷歌提供的所有答案解决这个问题。
var express = require('express');
var app = require('mongoose');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
mongoose.connect('mongodb://localhost:27017/mydb');
var Schema = new mongoose.Schema({
cr : String,
});
var user = mongoose.model('myc', Schema);
app.get('/view', function(req, res){
user.find({}, function(err, docs){
if(err) res.json(err);
else res.render('index', {users: docs});
});
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我使用的是node6.11.0和npm v 5.5.1 怎么解决这个?请求帮助
答案 0 :(得分:3)
您需要安装mongoose作为依赖项
运行此命令,
$ npm install mongoose --save
也替换
var app = require('mongoose');
到
var mongoose = require('mongoose');
答案 1 :(得分:0)
您需要确保安装了mongoose。尝试通过npm安装。
npm install mongoose --save
答案 2 :(得分:0)
将var app = require('mongoose');
替换为var mongoose = require('mongoose');
。我想,你已经安装了猫鼬。如果没有,请使用命令npm i mongoose --save