原谅我,我是新手。我不确定我的术语是否正确,但我想知道如何检索我的mongodb集合,称为' items'从我宁静的api。我需要提取整个集合数据,而不仅仅是一个查询,我没有运气找到一个体面的教程。
我使用标准的res.send('随机文本')命令测试了我的路线,所以我知道我的路线有效。但是当我尝试配置我的函数来检索我的集合并显示数据时,没有任何反应。我知道我的getList函数可能不正确,我觉得我的列表路径也是错误的,但我对错误所在的地方感到很茫然。
server.js
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const config = require('./config/database');
const todoList = require('./routes/list');
const port = 3000; //sets our local port to 3000
mongoose.connect(config.database);
mongoose.connection.on('connected', () => {
console.log('Connected to database ' + config.database);
});
mongoose.connection.on('error', (err) => {
console.log('Database error: ' + err);
});
app.use(cors());
app.use(bodyParser.json());
app.use('/todos', todoList);
app.listen(port, () => {
console.log('Server started on port ' + port);
});
模型/ list.js
const mongoose = require('mongoose');
const config = require('../config/database');
//user schema
const UserSchema = mongoose.Schema({
item: {
type: String
},
completed: {
type: Boolean
}
});
const Item = module.exports = mongoose.model('Item', UserSchema);
module.exports.getLists = function(callback) {
Item.find({callback});
}
击溃/ list.js
const express = require('express');
const router = express.Router();
const config = require('../config/database');
const List = require('../models/list');
//get all items in to do list
router.get('/lists', (req, res, next) => {
List.getLists((err, todos) => {
if(err) {
res.json({success: false, msg: 'No list found'});
} else {
res.json({
success: true,
list:
item: todos.item,
completed: todos.completed
});
}
});
});
答案 0 :(得分:0)
const express = require('express');
const router = express.Router();
const config = require('../config/database');
const List = require('../models/list');
//get all items in to do list
router.get('/lists', (req, res, next) => {
List.find({},(err, todos) => {
if(err) {
res.json({success: false, msg: 'No list found'});
} else {
res.json({
success: true,
list:
item: todos.item,
completed: todos.completed
});
}
});
});

看起来你正在使用猫鼬。所以只需用find替换getLists,因为你需要找到all,将filter子句保留为空。
答案 1 :(得分:0)
我明白了。我非常接近,但只是需要更好地了解module.exports如何使用参数以及回调如何工作。虽然你的答案不是我想要的,但是你让我找到了我的解决方案。谢谢你的回复!
需要从以下
更改我的代码模型/ list.ts
module.exports.getLists = function(callback) {
Item.find({callback});
}
路由/ list.ts
//get all items in to do list
router.get('/lists', (req, res, next) => {
List.getLists((err, todos) => {
if(err) {
res.json({success: false, msg: 'No list found'});
} else {
res.json({
success: true,
list:
item: todos.item,
completed: todos.completed
});
}
});
});
对此:
模型/ list.ts
module.exports.getLists = function(callback) {
const list = {}
Item.find(list, callback);
}
routes.list.ts
//get all items in to do list
router.get('/lists', (req, res, next) => {
List.getLists((err, todos) => {
if(err) {
res.json({success: false, msg: 'No list found'});
} else {
res.json({
success: true,
todos
});
}
});
});