简体   繁体   English

查询mongodb数据时在Node.js中出现错误?

[英]Getting error in nodejs when querying mongodb data?

I am getting error in nodejs console (see screenshot for more clarification). 我在nodejs控制台中遇到错误(更多说明请参见屏幕截图)。

In matches.js : 在matchs.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 : 在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 ie localhost://5000/home 我已经创建了包含matchs.js的模型文件夹,我正在尝试从mongodb访问数据以及何时将其显示为API端点(例如localhost://5000/home上的JSON数据

在此处输入图片说明

This is a basic JavaScript Object declaration error. 这是一个基本的JavaScript对象声明错误。 A JSON Object should look like this, JSON对象应如下所示:

{
   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. 另外,您的getmatches函数需要将限制作为第二个参数传递,您也需要在代码中传递该限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM