首页 > 解决方案 > 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
      });
    }
  });
});

标签: mongodbrest

解决方案


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.


推荐阅读