【发布时间】: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());
}
【问题讨论】: