我不断收到以下错误消息-TypeError:Todo.createDocument不是函数

时间:2019-03-30 00:23:15

标签: node.js mongoose mongoose-schema

每次我运行以下命令:

-$ curl -X POST -d '{ "text": "Item something", "done": true }' --header "Content-Type: application/json" localhost:3000/todos 

我收到以下错误:

  

TypeError:Todo.createDocument不是函数

app.js


const app = express();

const port = 3000;

const router = express.Router();

const bodyParse = require('body-parser');

const Todo = require('./todo');

router.get('/', (req, res) => res.send('Hello World!'));

router.route('/todos')
  .get((req, res) => {
    Todo.all(function(data){
      console.log('Retrieving all Todos');
      res.json(data);
    })

  })
  .post((req, res) => {
    Todo.createDocument(req.body, function(data){
      console.log('Todo created with id ${data._id}');
      res.json(data);
    })
  })


app.use('/', router);

app.listen(port, () => console.log('Example app listening on port 
${port}!'));

控制台日志

TypeError: Todo.createDocument is not a function
    at router.route.get.post (/Users/robbrown/node-todo-api/app.js:20:10)
    at Layer.handle [as handle_request] (/Users/robbrown/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/robbrown/node-todo-api/node_modules/express/lib/router/route.js:137:13)
    at next (/Users/robbrown/node-todo-api/node_modules/express/lib/router/route.js:131:14)
    at Route.dispatch (/Users/robbrown/node-todo-api/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/robbrown/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
    at /Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/robbrown/node-todo-api/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:317:13)
    at /Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/robbrown/node-todo-api/node_modules/express/lib/router/index.js:275:10)

这是我的todo.js文件,希望有人可以帮助我解决这个问题。过去几天,我一直在寻找答案。由于某种原因,我不会让我添加这个?错误消息大部分是代码。

## todo.js ##

const mongoose = require('mongoose');

mongoose.set('useFindAndModify', false);

mongoose.connect('mongodb://localhost:27017', { useNewUrlParse: true });

const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function(){
  console.log('Connected to MongoDb!');
});

const todoSchema = new mongoose.Schema({
  text: String,
  done: Boolean
})

todoSchema.statics.createDocumnent = function(params, callback){
  return Todo.create({ text: params.text, done: params.done },
    function (err, todo) {
      if (err) return handleError(err);
      callback(todo);
    }
  )
}
todoSchema.statics.all = function(callback){
  return Todo.find({}, function(err, todos){
    callback({ todos: todos });
  })
}
const Todo = mongoose.model('Todo', todoSchema);

module.exports = Todo;




1 个答案:

答案 0 :(得分:-1)

  

post((req,res)=> {      Todo.createDocument(需求主体,功能(数据){        console.log('Todo创建了ID $ {data._id}');        res.json(数据);      })    })

当您调用createDocument()函数时,问题就出在这里。在您的createDocument类中找不到函数Todo

现在,我在这里看不到Todo类是什么,但是如果它从父类(即DOMImplementation类)继承方法,则检查您的继承引用或添加createDocument函数在您的Todo类中。