mongodb - how to get a mongo collection with a restful api
问题描述
Forgive me I am new to this. I am not sure if my terminology is correct, but I am tying to figure out how to retrieve my mongodb collection called 'items' from my restful api. I need to pull the entire collection data and not just one query and I am not having any luck finding a decent tutorial.
I tested my route with the standard res.send('random text') command so I know my route works. But when I try to configure my functions to retrieve my collections and display the data, nothing happens. I know my getList function is probably not correct and I have a feeling my list route is wrong too, but I am at a loss on where the errors are.
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);
});
models/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});
}
routs/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
});
}
});
});
解决方案
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
});
}
});
});
Looks like you are using mongoose. So just replace the getLists with find and since you need to find all, leave the filter clause as empty.
推荐阅读
- c++ - 字体大小缩放问题
- python - 尝试使用键盘上的箭头键控制数组中数字 5 的特定位置
- python - 数据狗 API python 身份验证
- image - 如何使浏览器在 url 上使用查询字符串缓存图像
- typescript - Type for recursively remapping keys of "flattened" object into nested one (using template literals)
- python - 计算张量流概率中分布参数的梯度
- javascript - Vue方法返回属于单个用户的所有属性返回[object Object]
- c# - 系统输入 - 如何让我的游戏对象跳跃?
- javascript - Javascipt 替换数组中的确切数字
- r - 使用 For 循环从 R 发送电子邮件?使用 RDCOMClient