0

I'm coding up a simple demo rest-api, the default /api route work just fine, returning all the results(image below), but I'm hitting a roadblock. Which is that when I type: http://localhost:5050/api/interpol where /interpol maps to api/:params I'm getting back an empty array, instead of the document matching the artist.

As a side note, I'm running a 32bit Ubuntu 14.04(didn't realize it till after), so I had to downgrade both mongodb and mongoose to get them working together, as a consequence I'm using Mongodb 2.2.6 & Mongoose 4.0.8. Thanx in advance.

Model

// Dependencies
const mongoose = require('mongoose');
//Mongoose Schema
const Albums = new mongoose.Schema({
    title     : { type: String },
    releaseYr : { type: Date }
});

const MusicSchema = new mongoose.Schema({
    artist    : { type: String },
    albums    : [Albums]
});

mongoose.model('Music', MusicSchema);
module.exports = mongoose.model('Music');

Router

// Dependencies
const express = require('express');
const Music = require('../models/musicModel');
const musicRouter = express.Router();

// Routes
musicRouter.get('/', (req, res) => {
    Music.find({}, (err, artists) => {
        res.json(artists)
    })
});

musicRouter.route('/:artistName').get((req, res) => {
    const artistName = req.params.artistName;

    Music.find({ artist: artistName }, (err, artist) => {
        res.json(artist)
    });
});

module.exports = musicRouter;

App

// Dependencies
const express = require('express'),
      bodyParser = require('body-parser'),
      Mongoose = require('mongoose'),
      Joi = require('joi');

// Initiate express      
const app = express();

// Import musicRouter to handle api routes
const musicRouter = require('./api/musicRouter');

// Downgraded to Mongodb 2.2.6 & Mongoose 4.0.8
// Connect to Mongo database via Mongoose
Mongoose.connect('mongodb://localhost:27017/music1', {useNewUrlParser: true});

// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Send routes to musicRouter
app.use('/api', musicRouter);

module.exports = app;

Result of localhost:5050/api

Native
  • 73
  • 1
  • 5

1 Answers1

0

As with all JavaScript, Mongodb does a case-sensitive search.

You're querying { artist: 'interpol' } But referring from the image, you've saved it as Interpol.

So either send your request as /api/Interpol or make a case-insensitive search using regex as detailed here

1565986223
  • 6,420
  • 2
  • 20
  • 33