NodeJS TypeError:Model不是构造函数

时间:2017-12-10 16:10:18

标签: node.js angular mongoose

我有一个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());
}

1 个答案:

答案 0 :(得分:0)

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

所以改变:

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

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