其他分享
首页 > 其他分享> > mongoose 导出模块多页使用

mongoose 导出模块多页使用

作者:互联网

db.js

//db.js
const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/learn', {useNewUrlParser: true, useUnifiedTopology: true}, err => {
    if (err) console.log(err)
    else console.log('连接成功')
})

module.exports = mongoose

model.js

const mongoose = require('./db')

let UserSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    address: {
        type: String,
        required: true
    },
    age: Number
})

module.exports = mongoose.model('User',UserSchema,'userMes')

User.js

const Model = require('./model')

let admin = new Model({
    name: '李二狗',
    address: '四川省内江市东兴区',
    age: 18
})

admin.save(err => {
    if (err) console.log(err)
    else console.log('添加数据成功')

    Model.find({},(err, docs) => {
        if (err) console.log(err)
        else console.log(docs)
    })
})

标签:console,log,err,js,模块,mongoose,多页,true
来源: https://blog.csdn.net/weixin_47632722/article/details/121009868