TypeError:Task.find 不是 Node Js 中的函数

     2023-03-10     166

关键词:

【中文标题】TypeError:Task.find 不是 Node Js 中的函数【英文标题】:TypeError: Task.find is not a function in Node Js 【发布时间】:2021-01-08 03:31:18 【问题描述】:

我的模型有问题。我已连接到 MongoDB,并且我的服务器运行良好。 但是我无法通过 TaskController 路由。错误是由于模型还是 find() 函数。 我不知道如何解决它。 谁能帮帮我吗。 谢谢

tasks.js

const express=require('express');
const mongoose=require('mongoose');
const router=express.Router();

// const TaskModel=mongoose.model("taskslist");

// const Task=mongoose.model('Task');

const Task=require('../model/task.model');


router.get('/',(req,res)=>

   Task.find(,(err,docs)=>
       if(!err)
           res.render("tasks",
           );
       
    else
        console.log('Failed to retrieve the Course List: '+ err);

    
   );

);

module.exports=router;

app.js

const TasksController=require('./controller/tasks');

app.use(bodyParser.urlencoded(
    extended:true
));

app.get('/',(req,res)=>
    res.render('index',);
);

app.use(bodyParser.json());


app.set('views',path.join(__dirname,"/views/"));
app.set("view engine","hbs");
app.engine("hbs",expressHandleBars(
    extname:"hbs",
    defaultLayout:"mainlayout",
    layoutsDir:__dirname+"/views/layouts"
));

app.listen(3000,()=>
    console.log("server listening");
);

app.use("/tasks",TasksController);

task.mode.js

const mongoose=require('mongoose');

var taskSchema=new mongoose.Schema(

title:
    type:String,

,
isDone:

    type:Boolean,


);

module.exports=mongoose.model("Task",taskSchema);

【问题讨论】:

task.mode.js这是错字吗? 检查您导入的 task.model 是否与您的 task.model 文件名相同,这可能是错误的原因,因为您已将名称 task.mode.js 命名为文件并导入了 task.model 您确定在模型上运行查找查询吗?如果您正在导出架构,并且在架构上运行 find 不会暴露 find 功能。 这是一个拼写错误,但我给出了正确的名称。 那么如何从mongodb中取回数据??不使用find 【参考方案1】:

你错过了 mongoose.conect(db) 你可以试试这样的 -

         let taskjs=require('./task.js')
         let express=require('express');
         let mongoose=require('mongoose');
         let app;
         let db;
         let http
        let taskSchema
        let isDoneSchema
         port=process.env.PORT || 3000;
         http = require('http').createServer(app);
         http.listen(port);
         db="mongodb://localhost:27017/YourCollection";
         mongoose.connect(db);//you missed this one
         Schema=mongoose.Schema;
         
         titleSchema=new Schema(
         type:String 
         )
          
         isDoneSchema=new Schema(
         type:Boolean
         );
        task= mogoose.model("Task",titleSchema);
        isDone=mogoose.model("IsDone",isDoneSchema);
        taskjs(app,task,isDone)
        
        
        //and now in task.js
        
        module.exports=(app,task,isDone)=>
        app.use(bodyParser.urlencoded(
    extended:true
));

app.get('/',(req,res)=>
    res.render('index',);
);

app.use(bodyParser.json());


app.set('views',path.join(__dirname,"/views/"));
app.set("view engine","hbs");
app.engine("hbs",expressHandleBars(
    extname:"hbs",
    defaultLayout:"mainlayout",
    layoutsDir:__dirname+"/views/layouts"
    
));

/////
///now you can access the mongoDB on any route //////
app.get('/',(req,res)=>

   Task.find(,(err,docs)=>
       if(!err)
           res.render("tasks",
           );
       
    else
        console.log('Failed to retrieve the Course List: '+ err);

    
   );

);

        
        

【讨论】:

TypeError: datetime.datetime() 不是 JSON 可序列化的

】TypeError:datetime.datetime()不是JSON可序列化的【英文标题】:TypeError:datetime.datetime()isnotJSONserializable【发布时间】:2017-10-2419:19:43【问题描述】:我对MongoDB中的时间戳有疑问。所以情况是这样的:我读入了json并为它们添加了时间... 查看详情

TypeError: __init__() 得到了一个意外的关键字参数 '__no_builder' Kivy

】TypeError:__init__()得到了一个意外的关键字参数\\\'__no_builder\\\'Kivy【英文标题】:TypeError:__init__()gotanunexpectedkeywordargument\'__no_builder\'KivyTypeError:__init__()得到了一个意外的关键字参数\'__no_builder\'Kivy【发布时间】:2017-10-0810:27:26【... 查看详情

Strassen 的乘法--> TypeError: 'int' object has no attribute '__getitem__'

】Strassen的乘法-->TypeError:\\\'int\\\'objecthasnoattribute\\\'__getitem__\\\'【英文标题】:Strassen\'smultiplication-->TypeError:\'int\'objecthasnoattribute\'__getitem__\'Strassen的乘法-->TypeError:\'int\'objecthasnoattribu 查看详情

Uncaught TypeError: Object [object Object] has no method 'fancybox'

】UncaughtTypeError:Object[objectObject]hasnomethod\\\'fancybox\\\'【英文标题】:UncaughtTypeError:Object[objectObject]hasnomethod\'fancybox\'UncaughtTypeError:Object[objectObject]hasnomethod\'fancybox\'【发布时间】:2013-12-2503:48:58 查看详情

将 pandas 数据附加到 hdf 存储,得到 'TypeError: object of type 'int' has no len()' 错误

】将pandas数据附加到hdf存储,得到\\\'TypeError:objectoftype\\\'int\\\'hasnolen()\\\'错误【英文标题】:Appendingpandasdatatohdfstore,getting\'TypeError:objectoftype\'int\'hasnolen()\'error将pandas数据附加到hdf存储,得到\'TypeError:objectoftype\'int\'has 查看详情

IBM 的 Dojo 和 iWidgets:HandleEventException: TypeError: Object [object Array] has no method 'split'

】IBM的Dojo和iWidgets:HandleEventException:TypeError:Object[objectArray]hasnomethod\\\'split\\\'【英文标题】:DojoandiWidgetsbyIBM:HandleEventException:TypeError:Object[objectArray]hasnomethod\'split\'IBM的Dojo和iWidgets:HandleEvent 查看详情

PushPlugin TypeError: Object #<Object> has no method ''exec

】PushPluginTypeError:Object#<Object>hasnomethod\\\'\\\'exec【英文标题】:PushPluginTypeError:Object#<Object>hasnomethod\'\'execPushPluginTypeError:Object#<Object>hasnomethod\'\'exec【发布时间】:2014-11-2212:05:08【问题 查看详情

TypeError: jQueryxxxxxx 不是函数

】TypeError:jQueryxxxxxx不是函数【英文标题】:TypeError:jQueryxxxxxxisnotafunction【发布时间】:2016-03-2901:59:21【问题描述】:第一次打开移动应用主页时会返回错误"TypeError:Jqueryxxxxxxisnotafunction"虽然它显示了API回调结果"jQuery11130951250050095... 查看详情

TypeError: $(...).on 不是函数

】TypeError:$(...).on不是函数【英文标题】:TypeError:$(...).onisnotafunction【发布时间】:2013-03-1805:06:43【问题描述】:我正在使用jQuerylitebox。添加JS和CSS文件后,我收到此错误TypeError:$(...).onisnotafunctionatthislineinjsfile"return$(\'body\').on(\'cl... 查看详情

TypeError:不是构造函数

】TypeError:不是构造函数【英文标题】:TypeError:isnotaconstructor【发布时间】:2019-08-1413:08:10【问题描述】:我只是将代码用作有关JavaScript类的学习练习。代码产生“TypeError:SimpleLogger不是构造函数”。该类似乎已导出好的,但... 查看详情

TypeError: Object(...) 不是函数

】TypeError:Object(...)不是函数【英文标题】:TypeError:Object(...)isnotafunction【发布时间】:2018-10-2504:41:27【问题描述】:在ionic3、angularfire2v5上工作TypeError:Object(...)isnotafunctionatSwitchMapSubscriber.project(http://localhost:8100/build/ve 查看详情

TypeError:Ajv 不是构造函数

】TypeError:Ajv不是构造函数【英文标题】:TypeError:Ajvisnotaconstructor【发布时间】:2017-06-2511:50:57【问题描述】:我有这个类,我尝试使用new关键字实例化Ajv,但出现此错误:TypeError:Ajv不是构造函数代码:import*asAjvfrom"ajv";exportclas... 查看详情

TypeError:车把不是函数错误

】TypeError:车把不是函数错误【英文标题】:TypeError:handlebarsisnotafunctionerror【发布时间】:2022-01-1800:04:45【问题描述】:我是node.js的新手,我使用模板引擎express-handlebar,但我遇到的问题是:**TypeError:handlebarsisnotafunctionatObject.**... 查看详情

Javascript:TypeError:...不是构造函数

】Javascript:TypeError:...不是构造函数【英文标题】:Javascript:TypeError:...isnotaconstructor【发布时间】:2013-02-0703:54:12【问题描述】:我有一个TypeError问题:functionartist(name)this.name=name;this.albums=newArray();this.addAlbum=function(albumName)fo 查看详情

TypeError: $ (...).bootstrapTable 不是函数

】TypeError:$(...).bootstrapTable不是函数【英文标题】:TypeError:$(...).bootstrapTableisnotafunction【发布时间】:2019-10-0305:54:10【问题描述】:<tableid="selectCaseTable"class="tabletable-borderedtable-stripedtable-hover"><!--tablecode 查看详情

jQuery 表单验证显示 Uncaught TypeError: Object [object Object] has no method 'validate'

】jQuery表单验证显示UncaughtTypeError:Object[objectObject]hasnomethod\\\'validate\\\'【英文标题】:jQueryformvalidationshowingUncaughtTypeError:Object[objectObject]hasnomethod\'validate\'jQuery表单验证显示UncaughtTypeError:Object[objectObje 查看详情

Uncaught TypeError: Object #<an Object> has no method 'click'

】UncaughtTypeError:Object#<anObject>hasnomethod\\\'click\\\'【英文标题】:UncaughtTypeError:Object#<anObject>hasnomethod\'click\'UncaughtTypeError:Object#<anObject>hasnomethod\'click\'【发布时间】:2012-04-2013:18:45 查看详情

TypeError: $(...).timepicker 不是函数

】TypeError:$(...).timepicker不是函数【英文标题】:TypeError:$(...).timepickerisnotafunction【发布时间】:2015-10-2312:32:25【问题描述】:我遇到了Jquerytimepicker()的问题;功能这里是<linkhref="js/jqueryTimepicker/jquery.timepicker.css"rel="stylesheet"/> 查看详情