Mongodb TypeError:无法读取属性'文本及#39; 未定义的
我正在尝试使用上述方法在Todo模式中保存文档 模式和方法如下Mongodb TypeError:无法读取属性'文本及#39; 未定义的,mongodb,express,Mongodb,Express,我正在尝试使用上述方法在Todo模式中保存文档 模式和方法如下 router.post('/addtodo', (req, res, next) => { let todo = new Todos({ text: req.body.text, date: new Date(), }); Todos.addTodo(todo, (err, todos, next) => { if(err) throw err; res.json(todos); })
router.post('/addtodo', (req, res, next) => {
let todo = new Todos({
text: req.body.text,
date: new Date(),
});
Todos.addTodo(todo, (err, todos, next) => {
if(err) throw err;
res.json(todos);
});
});
但是当我尝试使用postman发布json对象时,它显示了这个错误
const todoSchema = mongoose.Schema({
text: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
const Todos = module.exports = mongoose.model('todos', todoSchema);
//Retrieve documents
module.exports.getTodos = (callback) => {
Todos.find(callback);
};
//Add document
module.exports.addTodo = (todo, callback) => {
Todos.create(todo, callback);
};
TypeError:无法读取属性';正文';未定义的
在router.post(C:\Users\shahaji.shinde\Desktop\practice\todo mean\router\todoroute.js:18:24)
在Layer.handle[作为handle\u请求](C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\Layer.js:95:5)
接下来(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\route.js:137:13)
在Route.dispatch(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\modules\express\lib\router\Route.js:112:3)
在Layer.handle[作为handle\u请求](C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\Layer.js:95:5)
在C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\index.js:281:22
在Function.process_参数处(C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node\modules\express\lib\router\index.js:335:12)
接下来(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\index.js:275:10)
在Function.handle(C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node\modules\express\lib\router\index.js:174:3)
在路由器上(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\index.js:47:12)
在Layer.handle[作为handle\u请求](C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\Layer.js:95:5)
在trim_前缀处(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node_modules\express\lib\router\index.js:317:13)
位于C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\index.js:284:7
在Function.process_参数处(C:\Users\shahaji.shinde\Desktop\practice\todo-mean\node\modules\express\lib\router\index.js:335:12)
接下来(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\router\index.js:275:10)
在expressInit(C:\Users\shahaji.shinde\Desktop\practice\todo mean\node\u modules\express\lib\middleware\init.js:40:5)
错误在第一个代码段中-第3行 您发布的信息让我想到了两个可能的原因: -这可能是因为您没有发送适当的内容类型:application/json(请检查)。 -服务器未准备好解析json: 为此,您应该有如下内容:
var bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json({limit: '10mb'}));
让我知道它是否有助于您检查req是否为null,body是否为null我正在使用postman发送POST请求,我确定body不是null我正在发送适当的内容类型您对解析器的看法是正确的。在主文件
app.js
中,我在路由器模块router.js
之后导入了解析器中间件body parser.json()
,这就是路由器模块无法访问解析器的原因。这有帮助!谢谢你
var bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json({limit: '10mb'}));