Javascript Express/Mongoose路由器:';转换为ObjectId的值“失败”;“未定义”;在路径上“_id"';
我在Express中有一个简单的API,允许用户在MongoDB数据库中“发布”和“删除”帖子标题。出于某种原因,当我添加一个帖子标题,然后“删除”它时,我在路径“\u id”处得到“Cast to ObjectId failed For value”undefined 在我创建帖子后调用“delete”时,似乎“\u id”不存在。但是,当我刷新页面,然后单击“delete”时,它会获得完全正确的“\u id”并删除条目 我在路由中是否做错了什么,没有生成“_id”并能够立即从帖子中提取Javascript Express/Mongoose路由器:';转换为ObjectId的值“失败”;“未定义”;在路径上“_id"';,javascript,node.js,express,mongoose,router,Javascript,Node.js,Express,Mongoose,Router,我在Express中有一个简单的API,允许用户在MongoDB数据库中“发布”和“删除”帖子标题。出于某种原因,当我添加一个帖子标题,然后“删除”它时,我在路径“\u id”处得到“Cast to ObjectId failed For value”undefined 在我创建帖子后调用“delete”时,似乎“\u id”不存在。但是,当我刷新页面,然后单击“delete”时,它会获得完全正确的“\u id”并删除条目 我在路由中是否做错了什么,没有生成“_id”并能够立即从帖子中提取 mo
module.exports = function(router) {
var Post = require('../models/post.js');
// middleware for the api requests
router.use(function(req, res, next) {
// do logging
console.log('something is happening.');
next(); // make sure we go to our next route and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
// all routes here
// routes that end in /posts
router.route('/posts')
// create a Post (accessed at POST http://localhost:7777/api/posts)
.post(function(req, res) {
var post = new Post();
post.postTitle = req.body.postTitle; // set the post name (comes from request)
console.log(post._id);
// save post and check for errors
post.save(function(err) {
if (err)
return res.status(300).send(err);
res.json({ message: 'post created!' });
});
})
// get all Posts (accessed at GET http://localhost:7777/api/posts)
.get(function(req, res) {
Post.find(function(err, posts) {
if (err)
return res.send(err);
res.json(posts);
});
});
// routes that end in /posts for specific id
router.route('/posts/:post_id')
// get the post with that id
.get(function(req, res) {
Post.findById(req.params.post_id, function(err, post) {
if (err)
return res.send(err);
res.json(post);
});
})
// update the post with that id
.put(function(req, res) {
Post.findById(req.params.post_id, function(err, post) {
if (err)
return res.send(err);
post.postTitle = req.body.postTitle;
// save the post
post.save(function(err) {
if (err)
return res.send(err);
res.json({ message: 'post updated!' });
});
});
})
// deletes the post with that id
.delete(function(req, res) {
Post.findOne({
_id:req.params.post_id
}).remove(function(x){
console.log("removed: ", x);
});
})
.patch(function(req, res) {
Post.findOne({
_id: req.body._id
}, function(err, doc) {
for (var key in req.body) {
dock[key] = req.body[key];
}
doc.save();
res.status(200).send();
});
});
}
/
/
在addPostItem中,在进行post调用之前,您将post添加到客户端模型列表中。推送到post数组上的新创建的post将不会有_id,因为它是在服务器上生成的,并且不会返回。当您将未定义的_id传递给此新post的服务器时,针对它的任何api调用都将因sa而失败我错了 当你刷新页面并调用get函数时,你所有的帖子都有一个_id,这样一切都能正常工作 遵循的一种典型模式是在帖子创建时将创建的id(或整个帖子)返回给客户端,然后将其添加到您的items数组中(如下所示): 您还需要重写POST请求以返回创建的实体:
post.save(function(err) {
if (err)
return res.status(300).send(err, o);
res.json(o);
});
一种变体是只返回.\u id。另一种变体是在客户端创建id,但这样会失去本机mongo ObjectID的一些优点。我在ui元素的任何地方都看不到_id。我认为从数据库中删除时应该获取_id。该_id自动生成并添加到MongoDB中文档,那么它不应该被检索到吗?删除时的req.params.post_id来自哪里?它来自API URL请求。我在PostStore中添加了delete函数。还有RestHelper文件。希望这有助于理解它。@Robert_Moskal有什么想法吗?我在上面添加了addPostItem()函数。
var $ = require('jquery');
module.exports = {
get: function(url) {
return new Promise(function(success, error) {
$.ajax({
url: url,
dataType: 'json',
success: success,
error: error
});
});
},
post: function(url, data) {
return new Promise(function(success, error) {
$.ajax({
url: url,
type: 'POST',
data: data,
success: success,
error: error
});
});
},
patch: function(url, data) {
return new Promise(function(success, error) {
$.ajax({
url: url,
type: 'PATCH',
data: data,
success: success,
error: error
});
});
},
del: function(url) {
return new Promise(function(success, error) {
$.ajax({
url: url,
type: 'DELETE',
success: success,
error: error
});
});
}
};
function addPostItem(post){
triggerListeners();
helper.post("/api/posts", post, function(res){
posts.push(res.data);
});
}
post.save(function(err) {
if (err)
return res.status(300).send(err, o);
res.json(o);
});