I am practicing to print the information stored in the db to html. Then, I used findOne() to search and print the information in the db, but an error occurred!. There is a problem with model.findOne() is not a function. I just started studying.
I'm not sure why this is happening. Please tell me the answer.
app.js
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
const createError = require('http-errors');
var morgan = require('morgan');
//MongoDB
var db = mongoose.connection;
db.on('error', console.error);
db.once('open', function(){
// CONNECTED TO MONGODB SERVER
console.log("Connected to mongod server");
});
mongoose.connect("mongodb+srv://test:[email protected]/test?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true });
//Model
var crodata = require('./model/cro');
var placedata = require('./model/place');
// app
var app = express();
// Routers
var main = require('./route/index');
var serch = require('./route/serch');
// view engine setup
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.set('views',__dirname+'/views'); // == app.set('views',path.join(__dirname, 'views'));
app.set('view engine','ejs');
app.engine('html',require('ejs').renderFile);
app.use('/',main);
app.use('/a',serch);
/model/cro.js
const mongoose = require('mongoose')
var Schema = mongoose.Schema;
var croSchema = new Schema(
{ index:String,content:String,data:String,like:String,place:String,tags:String}
)
module.exports = mongoose.model('cro',croSchema);
route/serch.js
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var crodata = require('../model/cro');
router.get('/',function (req,res,next) {
try{
crodata.findOne({})(function(err, result) {
if(err) { console.log(err)}
console.log(result);
//res.render('serch',result)
})
}
catch (e){
next(e);
}
});
module.exports = router;
question from:
https://stackoverflow.com/questions/65893575/i-got-an-error-saying-model-findone-is-not-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…