当我执行res.json(newschema)时,该属性不再显示在Postman上

时间:2018-07-27 22:47:45

标签: node.js web

当我在发布操作中执行res.json(newschema)时,这些属性不再显示在Postman和Mongodb中。

这是编队。路线

docker push org-name/image-name

这是Formation.models

action="{{url('urlportal')}}/@{{cliente.id}}"

这是我的app.js(端点)

const express = require('express');
const router = express.Router();
const Formation = require('../models/formations');
const mongoose = require('mongoose');


const config = require('../config/database');





router.get('/', function(req, res){
//Formation.getFormations(function(err, formations){
   // if(err)throw err;
  //  res.json(formations);
//});
Formation.find({})
.exec(function(err, Formations){
    if(err){
        console.log('error'); 
    }else{
        res.json(Formations);
    }
})
});



router.post('/', function(req, res){
    console.log('post a formations');
    var newFormation = new Formation();
    newFormation.title = req.body.title;
    newFormation.url = req.body.url;
    newFormation.description = req.body.description;
    newFormation.save(function(err, newFormation){
        if(err){
            console.log('error insertion');
        } else {
            res.json(newFormation);
            //res.json({success: true, msg:'user registred'});
        }
    });
});

这就是我在Postman中进行后期操作的方式

postman screenshot

1 个答案:

答案 0 :(得分:0)

save方法在回调中仅具有一个error参数。您将添加一个newFormations参数,该参数将始终是未定义的,并且在方法的范围内,您将在res.json调用中返回未定义的参数。要对其进行修复,请如下所示删除newFormation参数:

newFormation.save(function(err){
    if(err){
        console.log('error insertion');
    } else {
        res.json(newFormation);
        //res.json({success: true, msg:'user registred'});
    }
});