0

I'm new in node.js and try to do a cookbook restful API using nodejs. So there's a list and recipes which should be in specified list selected by id. I'm tryin' to get listId as params but got undefined value. The postman shows an error:

{ "errors": { "_listId": { "message": "Path _listId is required.", "name": "ValidatorError", "properties": { "message": "Path _listId is required.", "type": "required", "path": "_listId" }, "kind": "required", "path": "_listId" } }, "_message": "Recipe validation failed", "message": "Recipe validation failed: _listId: Path _listId is required.", "name": "ValidationError" }

So here's a:

//Recipe model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const RecipeSchema = new Schema({
    title: {
        type: String,
        required: true,
        minLength: 1,
        trim: true
    },
    _listId: {
        type: mongoose.Types.ObjectId,
        required: true
    }
});

module.exports = mongoose.model('Recipe', RecipeSchema);

The recipe routes:

const express = require('express');
const controller = require('../controllers/recipes');
const router = express.Router();


router.get('/' , controller.get);
router.post('/' , controller.post);
router.patch('/:id' , controller.update);
router.delete('/:id' , controller.delete);

module.exports = router;

Recipe controller:

const Recipe = require('../models/recipe.model');
//get all recipes from specific list
module.exports.get = (req, res) => {
    Recipe.find({
        _listId: req.params.listId
    }).then((tasks) => {
        res.send(tasks);
    });
    console.log(req.params.listId); //undefined
};

//create new recipes in the list specified by id
module.exports.post = (req, res) => {
    let newRecipe = new Recipe({
        title: req.body.title,
        _listId: req.params.listId
    });
    console.log(req.params.listId); //undefined
    newRecipe.save().then((newRecipeDoc) => {
        res.send(newRecipeDoc);
    }).catch((e) => {
        res.send(e)
    });
};

and app.js file where i configure the route

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const listRoutes  = require('./routes/list');
const recipeRoutes = require('./routes/recipe');
const keys = require('./config/keys');
const app = express();


mongoose.connect(keys.mongoURI,  {
    useNewUrlParser: true,
    useUnifiedTopology: true
})
    .then(() => console.log('MongoDB connected'))
    .catch(error => console.log(error));

app.use(bodyParser.json());
app.use('/lists', listRoutes);
app.use('/lists/:listId/recipes', recipeRoutes);

module.exports = app;

The get/post queries for List is working fine, but for Recipe i've got undefined value

grnje
  • 11
  • 1
  • Hi there! I believe this answers your question: https://stackoverflow.com/questions/25260818/rest-with-express-js-nested-router Please, let me know if it does not. – Tunmee Feb 27 '20 at 17:29

1 Answers1

0

ok, i found the problem may be it will help to someone. The route should be:

router.get('/:listId/recipes' , controller.get);
router.post('/:listId/recipes' , controller.post);
router.patch('/:listId/recipes:id' , controller.update);
router.delete('/:listId/recipes:id' , controller.delete);

and app.js route should be

app.use('/lists', recipeRoutes);

grnje
  • 11
  • 1