我在架构中收到一条错误消息,提示它未注册。请参阅下面的说明

时间:2019-03-29 11:11:43

标签: javascript node.js angular

所以。我试图将mongoDB与mongoose一起用于expressjs项目,但出现此错误

C:\ Users \ Sanjay \ Desktop \ BasicNodeJS \ node_modules \ mongoose \ lib \ index.js:434       抛出新的mongoose.Error.MissingSchemaError(name);       ^ MissingSchemaError:尚未为模型“博客”注册架构。 使用mongoose.model(name,schema)

请参见下面的源代码,让我知道需要进行哪些更改

这是架构代码。...blog.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let blogSchema = new Schema(
    {
        blogId: {
            type: String,
            unique: true
        },
        title: {
            type: String,
            default: ''
        },
        description: {
            type: String,
            default: ''
        },
        bodyHtml: {
            type: String,
            default: ''
        },
        views: {
            type: Number,
            default: 0
        },
        isPublished: {
            type: Boolean,
            default: false
        },
        category: {
            type: String,
            default: ''
        },
        author: {
            type: String,
            default: ''
        },
        tags: [],
        created: {
            type: Date,
            default: Date.now
        },
        lastModified: {
            type: Date,
            default: Date.now
        }
    };
); 

mongoose.model('Blog', blogSchema)

这是主要的js文件代码... index.js

const express = require('express');
const appConfig = require('./config/appConfig');
const fs = require('fs');
const mongoose = require('mongoose');

const app = express();


let modelsPath = './models';
fs.readdirSync(modelsPath).forEach(function (file){
    if(~file.indexOf('.js')) {
        require(modelsPath+'/'+file);
    };
});

app.listen(appConfig.port, () => {
    console.log('example app listening on port 3000');
    let db = mongoose.connect('mongodb://127.0.0.1:27017/blogAppDB');
});

1 个答案:

答案 0 :(得分:0)

您可以尝试以下一种方法:

模型:

(context.gotoScreen (context.getScreenById ("screen3"));
return true;)

CONTROLLER:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let blogSchema = new Schema(
   /*your schema fields*/
    };
); 

module.exports = mongoose.model('Blog', blogSchema) // here we export this schema to use in controller part

现在您可以使用Blog进行数据库操作