在我的express / node / mongodb服务器应用程序上,我的行为异常, 似乎只有当我使用findOneAndReplace方法调用PUT请求时,此请求看起来才是空的。
实际上我有以下错误消息用于验证方法:
此处是代码:
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const methodOverride = require('method-override');
const session = require('express-session');
const passport = require('passport');
const logger = require('morgan');
const cors = require('cors');
//MODEL
const Clienti = require('./model/clienti');
app.use(bodyParser.urlencoded({limit: '10mb', extended: false}));
app.use(methodOverride('_method'));
app.use(bodyParser.json({limit: '10mb'}));
//UPDATE
app.put('/api/aggiorna_cliente/:id', function(req, res, next) {
console.log(JSON.stringify(req.body)) --> N.B. i have the correct body rendition here
console.log(JSON.stringify(req.params.id))
Clienti.findOneAndReplace(
{ _id:req.params.id },
{
address:req.body.address,
brand:req.body.brand,
cap:req.body.cap,
city:req.body.city,
civico:req.body.civico,
email:req.body.email,
fiscalcode:req.body.fiscalcode,
provincia:req.body.provincia,
utente:req.body.utente
},
function (err, post) {
if (err) return next(err);
res.json(post);
});
});
此处为CLIENTI模型 const mongoose = require('mongoose');
const clientiSchema = mongoose.Schema({
utente:{
type: String,
required:true
},
cap:{
type: Number,
required:true
},
civico:{
type: String,
required:true
},
city:{
type: String,
required:true
},
address:{
type: String,
required:true
},
fiscalcode:{
type:String,
required:true
},
email:{
type:String,
required:true
},
brand:{
type:String,
required:true
},
provincia:{
province: String,
sigle: String
}
});
const Clienti = mongoose.model('Clienti',clientiSchema);
module.exports = Clienti
如您所见,几乎所有字段都是“必填”的,这导致了上图的验证问题。
但是,如果我删除了必填字段,则所有数据均为空(id除外)
我在angular中使用服务的最后方式
updateCustomer(customer){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.put(this.UpdateCustomer+'/'+customer._id, JSON.stringify(customer), {headers: headers})
.map((response: Response) => response.json())
}
那会是什么?
谢谢您的时间