Mongo api POST缺少字段

时间:2018-01-22 06:06:20

标签: node.js mongodb rest mean-stack

Mongo在api调用中没有插入我的JSON对象中的字段。我想我误解了JSON测试数据中的对象需要命名的内容。我已经尝试过非常好地结合了#34; ToDo"," todo"等,但无济于事。

我正在尝试tutorial for building a MEAN stack app。我设法构建它,但是,在Postman POST测试调用的主体中使用这个JSON对象

"todo": {
    title: "TaskTitle",
    description: "TaskDescription",
    status: "Not assigned"
}

当我在Postman中通过POSTing进行测试时,当我在Robo 3T中查看新创建的对象时,只会填充_iddate__v字段。

我希望在机器人3T中看到_iddate__vtitledescriptionstatus

我的模型看起来像这样:

var mongoose = require('mongoose')
var mongoosePaginate = require('mongoose-paginate')


var ToDoSchema = new mongoose.Schema({
    title: String,
    description: String,
    date: Date,
    status: String
})

ToDoSchema.plugin(mongoosePaginate)
const ToDo = mongoose.model('Todo', ToDoSchema)

module.exports = ToDo;

和执行发布的控制器本身显示请求主体未定义viz;

exports.createTodo = async function(req, res, next){
    // Req.Body contains the form submit values.
    console.log(toString(req.body.title));//----shows as [object Undefined] in console
    var todo = {
        title: req.body.title,
        description: req.body.description,
        status: req.body.status
    }

    try{

        // Calling the Service function with the new object from the Request Body

        var createdTodo = await TodoService.createTodo(todo)
        return res.status(201).json({status: 201, data: createdTodo, message: "Succesfully Created ToDo"})
    }catch(e){

我已经尝试了other SO posts,但无济于事......

2 个答案:

答案 0 :(得分:0)

这不是有效的JSON:

"todo": {
    title: "TaskTitle",
    description: "TaskDescription",
    status: "Not assigned"
}

这些是:

{
    "todo": {
        title: "TaskTitle",
        description: "TaskDescription",
        status: "Not assigned"
    }
}

{
    title: "TaskTitle",
    description: "TaskDescription",
    status: "Not assigned"
}

答案 1 :(得分:0)

@MondKin非常感谢你坚持我...问题是使用Postman进行测试并将体型格式化为“Text”,而不是“JSON(application / json)”

问题解决了......哇!