gpt4 book ai didi

node.js - blogSchema 未定义

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:10 26 4
gpt4 key购买 nike

我正在为博客应用程序创建 API,并且我将通过该 API 获取所有博客,但显示一些错误。我的app.js(这是API文件)和blogModel.js(这是架构文件)已经写在下面。请帮我找出错误。

///////////////////app.js/////////////////////////
//calling the module
var express = require('express');


//cretaing an instance
var app = express();


//calling body-parser and cookie-parser
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');


//calling mongoose module
var mongoose = require('mongoose');

var newBlog = [];


//using body-parser and cookie-parser
app.use(bodyParser.urlencoded({limit : '10mb', extended : 'true'}));
app.use(bodyParser.json({limit : '10mb', extended : 'true'}));
app.use(cookieParser());

//Defining configuration of mongoDB or at BlogApp it will create Database
var dbPath = "mongodb://localhost/BlogApp";


//Telling mongoDB to connect at dbPath or connect database
db = mongoose.connect(dbPath);


//Checking connection is open or not
mongoose.connection.once('open', function () {
console.log("connection is established");
})


//Including the schema file or Blog Model file
var Blog = require('./blogModel.js');


//To play with the data which will be store in blog or perform various functions on db using
//blogData variable
var blogData = mongoose.model('Blog');


//A simple route to check app is working or not correctly
app.get('/', function(req, res){
res.send("Application in on and it's working very fine. Stay in touch to get latest updates");
})

//Mian routes fro blog Application
//1.) Route to get all the blogs
app.get('/blogs',function(req, res) {
blogData.find(function(err,result){
if(err) {
console.log(err);
}
else {
console.log(result);
res.send(result);
}
})
});



//2.) Route to create a Blog



app.listen(3000, function(){
console.log("Listening on port 3000");
})



///////////blogModel.js///////////////////////
//Calling the module
var mongoose = require('mongoose');

//using the schema part of the module
var blogSchema = mongoose.Schema;

//creating an instance
var blogData = new blogSchema({

blogId : {type : String, default : ''},
blogCreatedOn : {type : Date, default : null},
blogHeading : {type : String, default : ''},
blogSubHeading : {type : String, default : ''},
blogBody : {type : String, default : ''},
imageUrl : {type : String, default : 'https://www.google.com'},
blogAuthorName : {type : String, default : ''}

});

//blogData is schema name
mongoose.model('Blog', blogData);

我的错误是:-=>blogSchema 未定义。

请帮助我。提前致谢

最佳答案

您应该导出模型才能需要它。所以在 blogModel.js 文件中更改以下内容:

mongoose.model('Blog', blogData);

对于这个:

module.exports = mongoose.model('Blog', blogData);

并在您的 app.js 中使用 Blog 来查询它:

var Blog = require('./blogModel.js');

// var blogData = mongoose.model('Blog');

//1.) Route to get all the blogs
app.get('/blogs',function(req, res) {
Blog.find(function(err,result){ ...

关于node.js - blogSchema 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47457964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com