如何修复猫鼬中的``.create不是函数''错误

纳乔·阿隆索

我正在尝试初始化seed.js文件以在数据库中启动一些内容,但是当我运行时,node bin/seed.js我不断'TypeError: Celebrity.create is not a function'

我尝试重新安装mongoosenpm软件包,我一直在寻找类似的问题,经过一段时间后,我觉得一切正常,但我找不到问题

这是我的相关app.js代码

mongoose
    .connect('mongodb://localhost/cinema', { useNewUrlParser: true })
    .then(x => {
        console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`)
    })
    .catch(err => {
        console.error('Error connecting to mongo', err)
    })

这是我的celebrity.model.js

const Schema = mongoose.Schema

const celebritySchema = new Schema(
    {
        name: String,
        occupation: String,
        catchPhrase: String
    },
    { timestamps: true }
)

const Celebrity = mongoose.model('Celebrity', celebritySchema)

module.exports = Celebrity

这是我的seed.js

const Celebrity = '../models/celebrity.model.js'

const dbName = 'cinema'
mongoose.connect(`mongodb://localhost/${dbName}`, { useNewUrlParser: true })

const celebrities = [
    {
        name: 'Alex',
        occupation: 'Parado',
        catchPhrase: 'Teo ayudameeee'
    }
]

Celebrity.create(celebrities, err => {
    if (err) {
        throw err
    }
    console.log(`Created ${celebrities.length} celebrities`)
    mongoose.connection.close()
})

这是错误

Celebrity.create(celebrities, err => {
          ^

TypeError: Celebrity.create is not a function
    at Object.<anonymous> (/home/nacho/Ironhack/week4/day5/de/lab-mongoose-movies/starter-code/bin/seed.js:31:11)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
    at internal/main/run_main_module.js:21:11

我希望种子在数据库中创建1个值,但出现错误

崇乐

您需要像这样在种子文件中要求模型文件:

const Celebrity = require('../models/celebrity.model.js');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:collection.group 不是猫鼬中的函数

猫鼬静态方法错误:不是函数

如何修复“ ...不是函数”错误?

如何修复猫鼬中的“铸造到嵌入式失败,无法实现价值”错误

提供所需值但猫鼬无法识别时如何修复猫鼬验证器错误

[猫鼬错误]:conn.openUri(...)。则不是函数

错误:TypeError:user.insertOne不是使用猫鼬的函数

toObject 不是转换猫鼬对象时的函数错误

类型错误:this.insertOne 不是函数,猫鼬静态

猫鼬模式方法:错误-模型方法不是函数

收到错误BlotClass.create不是ReactQuill中的函数

如何修复Javascript中的``Array.push不是函数''错误

如何修复Javascript中的“地图不是函数”错误

如何修复:react-redux中的'e不是函数'错误

猫鼬| 如何从此猫鼬模式中删除重复的错误

在我创建的新函数中调用我创建的函数时,如何修复不是函数错误

如何修复函数原型上的“不是函数”错误?

如何在猫鼬查询方法中返回错误?

使用res.json()时,我在猫鼬中收到内部错误500而不是404

如何使用虚拟猫鼬属性修复 TypeSrcipt 错误“对象可能‘未定义’”

如何修复 strcat 函数中的分段错误?

使用猫鼬的异步函数中的返回类型会产生错误

如何修复 this.setState 不是函数错误

如何修复“未捕获的类型错误:$(...).datepicker 不是函数”

如何使用 Mongoose 从模式创建模型?未捕获的类型错误:User.create 不是函数

如何在React中修复“ TypeError:category.map不是函数”错误

如何在Ionic 3中使用PayPal修复“ Object(...)不是函数错误”

如何修复Web刮板中的'page.goto不是函数'错误

函数如何在猫鼬模式中工作?