Nodejs - Mongoose - 回调只能工作一次

时间:2017-06-09 00:36:08

标签: node.js mongoose callback

我正在使用回调,它会进行第一次调用(POST),并且永远不会再次运行。我在邮递员中无法得到任何回复错误。 这是模态架构:

var mongoose = require('mongoose'); 
mongoose.Promise = require('bluebird'); 
var ModalitySchema = new mongoose.Schema({   
    name: {
    type: String,
    unique: true,
    required: true
  },
  type: {
    type: String,
    required: true
  },
    updated_at: { type: Date, default: Date.now },
  created_at: { type: Date, default: Date.now }
});

这是控制器中的呼叫:

var mongoose = require('mongoose');
var Modality = require('../models/modality').Modality;

/** create function to create Modality. */
exports.create = function (req, res) {
Modality =new Modality({name:req.body.name,type: req.body.type});  
Modality.save( function(err, result) {
        if (!err) {
            return res.json(result);
        } else {
          console.log(err);

            return res.send(err); // 500 error
        }
    });
};

和节点的外部: Outer of node

2 个答案:

答案 0 :(得分:0)

服务器遇到错误。您的模型导出错误,方法正确:

  

var Modality = require('../ models / modality')。model('Modality')。schema

答案 1 :(得分:0)

我是如此盲目以至于花了这么久才感到愚蠢,我正在拯救错误的模式:

Modality =new Modality({name:req.body.name,type: req.body.type});  
Modality.save( function(err, result) {....}
事实上它必须是

modality =new Modality({name:req.body.name,type: req.body.type});  
modality.save( function(err, result) {....}