【问题标题】:Im getting an Error icant resolve - Uncaught TypeError: _postSchema.Post is not a constructor我得到一个错误解决方案 - 未捕获的类型错误:_postSchema.Post 不是构造函数
【发布时间】:2020-02-12 21:52:26
【问题描述】:

我正在尝试从表单中获取值并将它们保存到本地 mongoDb,但出现错误。我将不胜感激任何帮助解决这个问题。 不知道问题出在表单定义上,还是babel解释代码的方式上

我的架构 只是另一个架构

import mongoose from 'mongoose'
const { Schema } = mongoose

const schemaPost = new Schema({ title: String, body: String })

export const Post = mongoose.model('post', schemaPost)

我的班级 redux-form 的基本定义

import { Field, reduxForm } from 'redux-form'
import { Post } from './schemaPost'

class CreatePost extends Component {
  renderInput = ({ input, label }) => {
    return (
      <div>
        <label> {label} </label>
        <input {...input} />
      </div>
    )
  }

  onSubmit = values => {
    new Post({
      title: values.title,
      body: values.body
    }).save()
  }

  render() {
    return (
      <div>
        <form onSubmit={this.props.handleSubmit(this.onSubmit)}>
          <Field name='title' component={this.renderInput} label='enter title' />
          <Field name='body' component={this.renderInput} label='enter body' />
          <button>Submit</button>
        </form>
      </div>
    )
  }
}

export default reduxForm({ form: 'createPost' })(CreatePost)

【问题讨论】:

    标签: reactjs mongodb mongoose redux-form


    【解决方案1】:

    尝试改变

    export const Post = mongoose.model('post', schemaPost)
    

    export const Post = mongoose.model('Post', schemaPost)
    

    【讨论】:

    • 检查错误是什么:model_name.save(function (err) { if (err) console.log(err); });
    • 我尝试...它没有到达 .save() 命令,它在构造函数定义中失败
    【解决方案2】:

    执行此操作以导出

    const Post = mongoose.model('post', schemaPost);
    export { Post };
    

    如果您要导出对象,这是正确的导出语法

    如果您想坚持现有的导出方法,则需要像这样更改您的导入和导出语句

    // export
    export mongoose.model('post', schemaPost);
    
    // import
    import Post from './schemaPost';
    

    【讨论】:

    • 我试试...基本相同的错误“Uncaught TypeError: _postSchema2.default is not a constructor”
    • 好吧,试试这个导入/导出,再次更新答案
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多