本文介绍了MongoDB-因值“创建"而强制转换为ObjectId失败在路径"_id"处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我尝试什么,我都无法摆脱这个错误,并且我的应用程序中还有其他一些功能可以创建Mongoose模型实例,这些实例看起来几乎完全像这样,并且都可以正常工作.

No matter what I try, I can't get rid of this error and I have several other features in my application that create instances of Mongoose Models which look almost exactly like this and they all work just fine.

CastError: Cast to ObjectId failed for value "create" at path "_id"

任何想法都将引发有关_id的错误吗?我的印象是_id值是由Mongoose自动生成的.我绝对保证我的帖子数据中没有id_id.只有titledescription通过.

Any idea what would be throwing this error about the _id? I was under the impression that the _id value was automatically generated by Mongoose. I absolutely guarantee that there is no id or _id in my post data. It is only the title and description coming through.

这是我的Project模式:

// Project Schema
var mongoose = require('mongoose');
var ProjectSchema = new mongoose.Schema({
    title: String,
    description: String
});

module.exports = mongoose.model('Project', ProjectSchema);

这是我的张贴路线.为了简洁起见,我将代码简化了.

And this is my post route. I kept the code short for brevity.

var express = require('express');
var router = express.Router();
var Project = require('../models/project');

router.post('/project/create', function(req, res, next) {
    var data = {
        title: req.body.title,
        description: req.body.description
    };

    Project.create(data, function(err, docs) {
        if (err) {
            return next(err);
        }

        if (!docs) {
            return res.send('Failed to create project');
        }

        res.send('Project created');
    });
});

module.exports = router;

最后,如果我从使用create方法更改为save方法,我仍然会遇到相同的错误.

Finally, if I change from using the create method to the save method, I still get the same error.

var project = new Project(data);

project.save(function(err) {
    if (err) {
        return next(err);
    }

    res.send('Project created');
});

推荐答案

我应该发布更多代码以帮助诊断此问题.事实证明,我的/project/create路线是问题所在.

I should have posted more of my code to help diagnosis this issue. It turns out my /project/create route was the issue.

我还有另一种途径来查看/project/:id项目.我以为我的路线的/create部分干扰了这种逻辑,所以我更改了路线中的路径,现在一切正常.

I also had another route to view a project that was /project/:id. I supposed the /create part of my route was interfering with this logic so I changed the paths in my routes and all is working just fine now.

// Before
router.get('/projects', projects); // view all projects
router.get('/project/:id, project); // view a project
router.post('/project/:id', projectUpdate); // update a project
router.post('/project/create', projectCreate); // create a project
router.post('/project/delete/:id', projectDelete); // delete a project

我的应用程序此功能的路线现在看起来像这样:

My routes for this feature of my app now look like this:

// After
router.get('/projects', projects); // view all projects
router.get('/project/:id', project); // view a project
router.post('/project/', projectCreate); // create a project
router.post('/project/:id', projectUpdate); // update a project
router.post('/project/delete/:id', projectDelete); // delete a project

我今天上了一课!感谢@Hypermattt的帮助.

I learned a lesson today! Thanks for the help @Hypermattt.

这篇关于MongoDB-因值“创建"而强制转换为ObjectId失败在路径"_id"处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:07