猫鼬返回空的JSON数组

时间:2018-09-26 08:21:01

标签: node.js mongodb express mongoose

Mongoose,表示,MongoDB返回一个空的json数组-[]。

我该如何解决?

已安装所有软件包。已经使用Robo 3t将数据输入到MongoDB中,所以我认为问题在于猫鼬没有以正确的方式从MongoDB请求数据。

server.js:位于/ app文件夹中

import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';

import Issue from './models/issue.js';

const app = express();
const router = express.Router();

app.use(cors());
app.use(bodyParser.json());

mongoose.connect('mongodb://localhost:27017/issues');

const connection = mongoose.connection;

connection.once('open', () => {
    console.log('MongoDB database connection established successfully!');
});

router.get("/issues", function(req, res) {
    Issue.find({})
    .exec(function (err, issues) {
        if (err)
            console.log(err);
        else
            console.log("successfully requested");
            res.json(issues);
    });
});

Index.js:位于/ app / models文件夹中

import mongoose from 'mongoose';

const issueSchema = new mongoose.Schema({
    title: {
        type: String
    },
    responsible: {
        type: String
    },
    description: {
        type: String
    },
    severity: {
        type: String
    },
    status: {
        type: String,
        default: 'Open'
    }
});

module.exports = mongoose.model('Issue', issueSchema, 'Issues');

1 个答案:

答案 0 :(得分:0)

如下所示,将const { GraphQLServer } = require('graphql-yoga') const typeDefs = ` type Query { hello(name: String): String! } ` const resolvers = { Query: { hello: (_, { name }) => `Hello ${name || 'World'}`, }, } const server = new GraphQLServer({ typeDefs, resolvers, skipValidation: true }) server.express.use('/api/stripe/webhooks', (req, res) => { // Handle your callback here !!!! res.status(200).send(); }) server.start(() => console.log('Server is running on localhost:4000'))更改为router,并确保您的集合名称在数据库中为app别忘了重新启动服务器

Issues
相关问题