gpt4 book ai didi

javascript - 查询mongodb数据时nodejs出错?

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:44 26 4
gpt4 key购买 nike

我在 Nodejs 控制台中收到错误(请参阅屏幕截图以获取更多说明)。

在 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);
}

在 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....')

我已经制作了包含 matches.js 的模型文件夹,我正在尝试从 mongodb 访问数据以及何时将其显示为 API 端点上的 JSON 数据,即 localhost://5000/home

enter image description here

最佳答案

这是一个基本的 JavaScript 对象声明错误。 JSON 对象应该如下所示,

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

您没有使用 ,,而是使用了 ;,这会引发语法错误。使用这个代替,

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

此外,您的 getmatches 函数需要将限制作为第二个参数传递,您也需要在代码中传递该限制。

关于javascript - 查询mongodb数据时nodejs出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com