我已经使用angular-fullstack-generator 4.0.1构建了ExpressJS API端点。当我对现有数据发送PUT请求时,API返回200,但MongoDB中没有变化 - 数据保持不变。我没有PUT返回200,数据没有变化。 GET和POST工作正常,没有任何问题。
我对MEAN堆栈很新,所以我可能会遗漏一些非常简单的东西。
服务器/ API /响应/ index.js:
'use strict';
var express = require('express');
var controller = require('./response.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;
服务器/ API /响应/ response.model.js:
'use strict';
import mongoose from 'mongoose';
var ResponseSchema = new mongoose.Schema({
created_at: Date,
userResponse: Object
});
export default mongoose.model('Response', ResponseSchema);
服务器/ API /响应/ response.controller.js:
//...
// Upserts the given Response in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
delete req.body._id;
}
return Response.findOneAndUpdate(req.params.id, req.body, {upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
//...
GET api / responses / 57aa6b6845078181549f703f结果:
{"_id":"57aa6b6845078181549f703f","userResponse":{"name":"BOB","age":"30","existsOtherPerson":"There will be me and others","howManyOthers":"","ownedHomeBefore":"","shortSaleOrForeClosure":"","foreClosureYearsAgo":"","proceed":"","deadend":"","shortSaleYearsAgo":"","proceed2":"","deadend2":"","neitherShortOrForeClosureOverThreeYearsAgo":"","haveBankruptcy":"","bankruptcyType":"","dischargeYears":"","proceed3":"","deadend3":"","incomeSource":"","timeAtJobMoreThanTwoYears":"","timeAtJobMonths":"","previousEmployerPaidHow":"","previousEmployerGrossPaycheck":"","2WeekPaycheck":"","havePartTimeJob":"","partTimeJobMoreThan2Years":"","partTimeJobPaidHow":"","partTimeJobGrossPaycheck":"","otherIncomeSources":"","hasCashAssets":"","cashAssetValue":"","hasPropertyAssets":"","stillOwnPropertyAssets":"","propertyAssetMonthlyPayment":"","propertyAssetYearsOfPayments":"","hasRetirementAssets":"","retirementAssetsValue":"","monthlyRent":"","monthlyCreditCard":"","hasCarPayment":"","monthlyCar":"","hasStudentLoans":"","monthlyStudentLoan":"","deferredTime":"","hasOtherMonthlyPayments":"","creditScore":"","coApplicantReady":"","coApplicantFirstName":"","coApplicantEmail":"","phoneNumberFutureContact":"","phoneNumber":"","otherCoApplicantsReady":"","firstCoApplicantFirstName":"","firstCoApplicantEmail":"","secondCoApplicantFirstName":"","secondCoApplicantEmail":"","phoneNumberFutureContact2":""},"__v":0}
现在为PUT。
PUT api / responses / 57aa6b6845078181549f703f 响应标题
HTTP/1.1 200 OK
X-Powered-By: Express
X-XSS-Protection: 1; mode=block
X-FRAME-OPTIONS: SAMEORIGIN
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Set-Cookie: XSRF-TOKEN=SmZRj04bTAc1zhijJcLgy4QKncvNxLjQ%2Fy7pI%3D; Path=/
Content-Type: application/json; charset=utf-8
ETag: W/"574-DCUb4YAsIHkEpBlFCHSNCQ"
Vary: Accept-Encoding
Content-Encoding: gzip
Date: Wed, 10 Aug 2016 03:12:26 GMT
Connection: keep-alive
Transfer-Encoding: chunked
PUT api / responses / 57aa6b6845078181549f703f 请求标题
PUT /api/responses/57aa6b6845078181549f703f HTTP/1.1
Host: app.uxweb.io
Connection: keep-alive
Content-Length: 1415
Accept: application/json, text/plain, */*
Origin: [obfuscated]
X-XSRF-TOKEN: fDud34CMs0GLqzSXkrJwKAlM1ixcaQ9jPVssE=
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: [obfuscated]
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: connect.sid=s%3AcGP8M4iYpBJfABQke0vbr_X7oRoghAkr.Y%2B%2Fd7eroLXstvl0a3EszESDlJHIje%2BcFN6z7hozoK64; _gat=1; _ga=GA1.3.1959951694.1470779005; XSRF-TOKEN=fDud34CMs0GLqzSXkrJwKAlM1ixcaQ9jPVssE%3D
PUT api / responses / 57aa6b6845078181549f703f 请求有效负载 请注意我是如何做出一些更改的(添加了howManyOthers等)
{"_id":"57aa6b6845078181549f703f","userResponse":{"name":"Kite","age":"30","existsOtherPerson":"There will be me and others","howManyOthers":"2 more people","ownedHomeBefore":"","shortSaleOrForeClosure":"","foreClosureYearsAgo":"","proceed":"","deadend":"","shortSaleYearsAgo":"","proceed2":"","deadend2":"","neitherShortOrForeClosureOverThreeYearsAgo":"","haveBankruptcy":"","bankruptcyType":"","dischargeYears":"","proceed3":"","deadend3":"","incomeSource":"","timeAtJobMoreThanTwoYears":"","timeAtJobMonths":"","previousEmployerPaidHow":"","previousEmployerGrossPaycheck":"","2WeekPaycheck":"","havePartTimeJob":"","partTimeJobMoreThan2Years":"","partTimeJobPaidHow":"","partTimeJobGrossPaycheck":"","otherIncomeSources":"","hasCashAssets":"","cashAssetValue":"","hasPropertyAssets":"","stillOwnPropertyAssets":"","propertyAssetMonthlyPayment":"","propertyAssetYearsOfPayments":"","hasRetirementAssets":"","retirementAssetsValue":"","monthlyRent":"","monthlyCreditCard":"","hasCarPayment":"","monthlyCar":"","hasStudentLoans":"","monthlyStudentLoan":"","deferredTime":"","hasOtherMonthlyPayments":"","creditScore":"","coApplicantReady":"","coApplicantFirstName":"","coApplicantEmail":"","phoneNumberFutureContact":"","phoneNumber":"","otherCoApplicantsReady":"","firstCoApplicantFirstName":"","firstCoApplicantEmail":"","secondCoApplicantFirstName":"","secondCoApplicantEmail":"","phoneNumberFutureContact2":""}}
快速控制台
(node:23321) Warning: a promise was created in a handler but was not returned from it
PUT /api/responses/57aa6b6845078181549f703f 200 23.753 ms - -
当我再次进行GET时,它在PUT之前的结果相同。我能做错什么?
请告诉我如何进一步澄清这个问题。
编辑:一旦我压缩我的模型和有效负载,PUT就可以工作了。所以问题在于Mongoose如何更新对象。