gpt4 book ai didi

node.js - Mongoskin不会从nodejs应用程序中的mongodb获取一些结果

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:47 25 4
gpt4 key购买 nike

我想通过 mongoskin 从 mongodb 读取一些数据。但是当我尝试调用 localhost:3000/tasks 时,nodejs 会抛出错误

在我的数据库“todo”中,“任务”集合中有一些项目。当我启动应用程序时,控制台中的结果工作正常。

有人能看出我哪里写错了吗?

错误消息:

500 TypeError: Cannot read property 'tasks' of undefined
at exports.list (/Users/piet/Websites/privat/mongo/routes/tasks.js:7:8) at callbacks (/Users/piet/Websites/privat/mongo/node_modules/express/lib/router/index.js:164:37)
at param (/Users/piet/Websites/privat/mongo/node_modules/express/lib/router/index.js:138:11)
at pass (/Users/piet/Websites/privat/mongo/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/Users/piet/Websites/privat/mongo/node_modules/express/lib/router/index.js:173:5)
at Object.router (/Users/piet/Websites/privat/mongo/node_modules/express/lib/router/index.js:33:10)
at next (/Users/piet/Websites/privat/mongo/node_modules/express/node_modules/connect/lib/proto.js:193: 15)
at Object.methodOverride [as handle] (/Users/piet/Websites/privat/mongo/node_modules/express/node_modules/connect/lib/middleware/me thodOverride.js:48:5)
at next (/Users/piet/Websites/privat/mongo/node_modules/express/node_modules/connect/lib/proto.js:193: 15)
at Object.urlencoded [as handle] (/Users/piet/Websites/privat/mongo/node_modules/express/node_modules/connect/lib/middleware/ur lencoded.js:49:37)

我的app.js:

var express = require('express');
var routes = require('./routes');
var tasks = require('./routes/tasks');
var http = require('http');
var path = require('path');

var mongo = require("mongoskin");
var db = mongo.db('localhost:27017/todo?auto_reconnect=true', {
safe:true })
.collection('tasks')
.find()
.toArray(function(error, items){
console.log(items);
})

var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/tasks', tasks.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

/routes/tasks.js下的路由文件

exports.list = function(req, res, next){
req.db.tasks.find({
completed: false
}).toArray(function(error, tasks){
res.render('tasks', {
title: "ToDo list",
name: tasks = tasks || []
})
})
};

最佳答案

使用 28017 作为端口 ID。您可以在“req.db”中检查您得到的内容。也许这是未定义的。

关于node.js - Mongoskin不会从nodejs应用程序中的mongodb获取一些结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com