【发布时间】:2018-07-30 12:44:35
【问题描述】:
无法使用 PUT 方法更新 mongoDB 记录。参数传递正确,我猜查询中一定有问题。
架构
let Boards = new mongoose.Schema({
title: String,
description: String,
lastUploaded: Number,
owner_id: String
});
服务器:
module.exports.updateTime = function (req, res) {
let board = new Board();
let id = new mongoose.Types.ObjectId(req.body._id);
let myquery = { _id: id };
let newvalues = { lastUploaded: req.body.time };
console.log("New time: " + req.body.time); //Number recieved
console.log("Id: " + req.body._id); //String recieved
board.findByIdAndUpdate(myquery, newvalues, function (err, response) {
if (err) {
response = { success: false, message: "Error updating data" };
} else {
response = { success: true, message: "Data updated" };
}
res.json(response);
});
board.close();
};
客户:
public updateTime(updateOptions: UpdateTime): Observable<any> {
let headers = new Headers;
let URI = `${apiUrl}/updateTime`;
headers.append('Content-Type', 'application/json');
return this.http.put(URI, updateOptions, { headers: headers })
.map(this.extractData)
.catch(this.handleError);
}
路由器:
router.put('/updateTime', ctrlContent.updateTime);
终于通过.catch(this.handleError);给了我空响应错误
【问题讨论】:
-
你能贴出put url吗?当您 PUT 时,您是否在 url 中传递了 id ?还有 console.log(req.body)
-
这看起来不像一个完整的例子。路线在哪里? findByIdAndUpdate 回调中会发生什么?看起来不错。
-
@RobertMoskal 通过浏览器网络检查请求尚未完成。所以它不能通过 findByIdAndUpdate。
-
err的值是多少? -
@willmaz console.log(req.body) 给出所有传递的参数,这不是问题。似乎问题出在 findByIdAndUpdate
标签: node.js angular mongodb express