【问题标题】:NodeJS TypeError: Model is not a constructorNodeJS TypeError:模型不是构造函数
【发布时间】:2018-05-24 06:06:13
【问题描述】:

我有一个 Angular5 应用程序正在向我的 NodeJS Api 发出请求,特别是我正在尝试发出 put 请求。第一次工作正常,但第二次调用它时,它说我的模型不是构造函数。我在 NodeJS 中使用 Mongoose。

这是 NodeJS 中的一段代码:

var Aviamentos = require('./../models/Aviamentos');
router.put('/:receitaId/prescricoes/:prescricaoId/aviar', function (req, res) {
    let aviamento = new Aviamentos({ // <-- It fails on this line the second time
        Farmaceutico: req.body.Farmaceutico,
        Data: new Date(),
        Quantidade: req.body.Quantidade,
    });
    ...

这是我的模型:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var aviamentoSchema = mongoose.Schema({
    Farmaceutico: {
        type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
    },
    Data: Date,
    Quantidade: Number
});

module.exports = mongoose.model( 'Aviamentos', aviamentoSchema);

我第一次在 AngularJS 端执行请求,它工作正常,但第二次出现此错误:TypeError: Aviamentos is not a constructor

这是我在 Angular5 中的功能:

aviarPrescricao(idReceita: string, idPrescricao: string, Quantidade: number){
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let body = {
        Quantidade: Quantidade,
        Farmaceutico: '5a08540a243ad41cf0659376'
    };
    return this.http.put(this.baseURL + '/' + idReceita + '/prescricoes/' + idPrescricao + '/aviar', JSON.stringify(body), { headers: headers })
        .map((response: Response) => response.json());
}

【问题讨论】:

    标签: node.js angular mongoose


    【解决方案1】:

    在您的模型 js 中,您已经声明了 Schema:

    所以改变:

    var aviamentoSchema = mongoose.Schema({
        //code
    });
    

    var aviamentoSchema = new Schema({
        //code
    });
    

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 1970-01-01
      • 2019-03-24
      • 2019-08-14
      • 1970-01-01
      • 2018-06-27
      • 2016-04-16
      相关资源
      最近更新 更多