0

I am getting error in nodejs console (see screenshot for more clarification).

In matches.js :

const mongoose = require('mongoose');

let Schema = mongoose.Schema;

const matchSchema = new Schema({

    match_id:{
        type:Number;     <---- line 8
        required:true;
    },

    season:{
        type:Number;
        required:true;
    }

.....
.....
});

const matches = mongoose.model('matches', matchSchema);

module.exports = matches;

// Get matches
module.exports.getmatches = (callback, limit) => {
    matches.find(callback).limit(limit);
}

In app.js :

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const app = express();

matches = require('./models/matches');

mongoose.connection.openUri('mongodb://localhost:27017/IPL');
const db = mongoose.connection;

app.get('/home', (req, res) => {
    matches.getmatches((err, match) => {

        if(err){
            throw err;
        }

        res.json(matches);

    });
});

app.listen('5000');
console.log('Running on port 5000....')

I have made model folder which contains matches.js I am trying to access data from mongodb and when to display it as JSON data on API endpoint i.e localhost://5000/home

enter image description here

3
  • 1
    On line 8-9, you will have to replace the semicolons with commas (to make it a valid json object) Commented Jan 20, 2018 at 15:44
  • @Brain done still gives error Commented Jan 20, 2018 at 15:48
  • @brain got it if you give short answer I can upvote :) Commented Jan 20, 2018 at 15:55

1 Answer 1

3

This is a basic JavaScript Object declaration error. A JSON Object should look like this,

{
   match_id: {
     type: Number,    //see the comma there?
     required: true
   }
}

Instead of using ,, you have used ; which will throw a syntax error. use this instead,

const matchSchema = new Schema({
    match_id: {
        type:Number,
        required:true
    },
    season: {
        type:Number,
        required:true
    }
    .....
    .....
});

Also, your getmatches function requires a limit passed as the second parameter, you need to pass that as well in the code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.