猫鼬.create / find不返回错误或结果

时间:2018-10-13 22:29:29

标签: express mongoose request response

我对Mongoose还是很陌生,我正在尝试创建一个MERN应用程序,该程序可以保存和查看文章。

在我的 server.js 文件中,

// Dependencies
const 
    bodyParser = require('body-parser'),
    dotenv = require('dotenv'),
    express = require('express'),
    mongoose = require('mongoose')
    path = require('path'),

// Hook Mongoose models to the Article variable.
Article = require('./client/models/Article');

// -------------------- MongoDB Configuration --------------------

// If deployed, use the deployed database. Otherwise use the local NYTReact database
const MONGODB_URI = "mongodb://localhost/nytreact" || process.env.MONGODB_URI;

// Set mongoose to leverage built-in JavaScript ES6 Promises
// Connect to the Mongo DB
mongoose.Promise = Promise;
mongoose.connect(MONGODB_URI, { useNewUrlParser: true });

// -------------------- Express Configuration --------------------

// Default PORT to be used
const PORT = 8080;

// Initialize Express
const app = express();

// Use body-parser for handling form submissions
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Use express.static to serve the public folder as a static directory
app.use(express.static(path.join(__dirname, 'public')));

// -------------------- POST and GET calls --------------------
app.get("/api/Article", function(request, response){
    console.log("SERVER REQUEST TO GET :", request.body);
    Article.find(), function (err, articles) {
        if(articles === null) {
           console.log('No results found');
        }
        if (err) console.log(err);
      }
});

app.post("/api/Article", function(request, response){
    console.log("SERVER REQUEST TO POST : ", request.body);
    Article.create(request.body, function(error, result){
        if (error) {
            console.log(error);
        }
        else {
            console.log(result);
        }
    });
});

// -------------------- Start the Server! --------------------

// Start the server
app.listen(PORT, function() {
    console.log("Running on " + PORT);
});

我的猫鼬模式如下

var mongoose = require("mongoose");

var Schema = mongoose.Schema;

var ArticleSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    summary: {
        type: String
    },
    writer: {
        type: String
    }, 
    date: {
        type: String
    },
    url: {
        required: true,
        type: String,
        unique: true
    }
});

var Article = mongoose.model("Article", ArticleSchema);

module.exports = Article;

在我的bash上,我能够看到我的console.logs来获取/发布服务器请求,但是无论如何都没有错误或结果。

get和post调用中的

console.log(response)也没有返回任何内容。我已经花了几个小时试图解决响应电话,因此,非常感谢任何反馈。如果需要查看其他文件,请也告诉我。

0 个答案:

没有答案