完全卡住了。 (MongoDB / MERN问题)

时间:2018-07-25 17:19:52

标签: node.js mongodb reactjs express

我正在创建一个应用程序,现在我正在开发一项功能,该功能接受用户输入并将其存储在数据库中。我已经在其他项目上做了很多次,但是遇到了一个我无法解决的问题。

到目前为止,当用户键入他们的信息并按Enter键时,数据将发送到后端,并开始向端点移动。我有一个使用“创建”功能设置的控制器。如下:

   const db = require('../database/models');

module.exports = {

// create: function(req, res) {
//     console.log('hit controller');
//     db.Listing
//       .create(req.body)
    //       .then(dbModel => res.json(dbModel))
    //       .catch(err => res.status(422).json(err));
    //   }

    create: function(req, res) {
    console.log('!!!!HERE:CONTROLLERS' + req.body.receiver);
    db.Listing
        .create(req.body)
        .then(function(dbModel) {
            console.log(dbMessage);
            res.json(dbModel)
        })
        .catch(function(err) {
            res.json(err);
        });
    }
    };

我有两个函数,因为我为每个函数尝试了一些不同的方法,但结果相同。一旦到达这一点,我会收到以下错误(“命中控制器”只是create函数中的console.log):

    'hit controller'
TypeError: Cannot read property 'create' of undefined

我在浏览器的控制台中也收到以下错误:xhr.js:178 POST http://localhost:3000/listing/ 500(内部服务器错误)从saveListing错误出现错误:在createError(createError(createError.js)上请求失败,状态码为500 :16)在XMLHttpRequest.handleLoad(xhr.js:77)的定居点(settle.js:18)

哪个来自我的addListing页面:

handleFormSubmit = (event) => {
 event.preventDefault();
 console.log('hit');
 console.log("state in handle submit", this.state.title)
    if (
        this.state.title &&
        this.state.description &&
        this.state.duration &&
        this.state.datesAvailable &&
        this.state.tags 
    ) {
        API.saveListing({ 
            title: this.state.title,
            description: this.state.description,
            duration: this.state.duration,
            datesAvailable: this.state.datesAvailable,
            tags: this.state.tags
        }) 
            .then(res => console.log('success'))
            .catch((err) => console.log("err from saveListing", err));
    }
};

这是我的模型文件:

var mongoose = require("mongoose");


var Schema = mongoose.Schema;


var ListingSchema = new Schema({
  // `title` is of type String
  title: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  duration: {
    type: String,
    required: true
  },
  datesAvailable: {
    type: String,
    required: true
  },
  hashtags: {
    type: Array,
    required: true
  }
});


var Listing = mongoose.model("Listing", ListingSchema);


module.exports = Listing;

因此,请重述:当用户点击“提交”按钮时,表单运行一个函数,使用所需的路径点击API,到达服务器,然后到达发布请求的路径,然后到达llistingController。从我的Listing DB模型中提取信息时,为帖子创建功能。一旦达到创建功能,便会弹出。数据显然是不确定的,但我不知道为什么,也无法弄清楚。

我觉得我遇到的事情可能只是他妈的而已,但是我无法为自己的生活弄清楚。此外,如果有人知道一些不良的资源来添加到我的mongo / express / react知识中,我很乐意看到它。

2 个答案:

答案 0 :(得分:0)

您正在从模型文件导入db,但是该文件正在导出Listing。导入Listing并使用Listing.create代替db.Listing.create

答案 1 :(得分:0)

正如223seneca所说,我将控制器中的db.Listing.create更改为Listing.create。然后,我需要将控制器文件中的清单列出为确切的文件,而不只是模型文件夹中的清单('const Listing = require('../ database / models / Listing');'而不是'const Listing = require('。 ./database/models');'。

那是我所需要的!

相关问题