Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

node.js - I got an error saying model.findOne() is not function

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

syntax for findOne is incorrect if you want use findOne based on callback use like this:

    crodata.findOne({} , function(err, result) {
        if(err) { console.log(err)}
        console.log(result);
        //res.render('serch',result)
    }) 

or if you want use async/await use like this

router.get('/',async function (req,res,next) {
try{
      
  let result = await crodata.findOne({})
  console.log(result)
        //res.render('serch',result)      
}
catch (e){
    next(e);
}
});

note : if you want find all data in collection use find, if you want find one object, create a filter for findOne({place:"somthing"}) because findOne without filter return first document in the collection


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...