gpt4 book ai didi

javascript - Actor 错误: Cast to ObjectId failed for value "id goes here" at path "_id" for model "Article"

转载 作者:行者123 更新时间:2023-12-01 04:09:51 28 4
gpt4 key购买 nike

我遇到了一些奇怪的事情,想知道是否有人可以帮助我。我有一个 MEAN 应用程序,能够完美地从 MongoDB 检索所有记录,但是当我尝试按 id 仅检索 1 条记录时,我收到了 CastError。我什至尝试降级我的 Mongo 版本,但问题仍然存在。任何帮助都会很棒。谢谢

var express = require('express');
var router = express.Router();

var Article = require('../models/article');


/* GET users listing. */
router.get('/', function(req, res, next) {
Article.getArticles(function (err, articles) {
if (err) {
console.log(err);
} else {
res.json(articles);

}
});
});

router.get('/:id', function(req, res, next) {
Article.getArticleById(req.params.id, function (err, article) {
if (err) {
console.log(err);
} else {
res.json(article);
}
});
});

module.exports = router;

var mongoose = require('mongoose').set('debug', true);

var articleSchema = mongoose.Schema({

title: {
type: String,
index: true,
required: true
},
body: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
category: {
type: String,
index: true,
required: true
}
});

var Article = module.exports = mongoose.model('Article', articleSchema);

// Get all articles
module.exports.getArticles = function (callback) {
Article.find(callback);
};

// Get article by ID
module.exports.getArticleById = function (id, callback) {
Article.findById(id, callback);

};

[CastError: Cast to ObjectId failed for value "58753da5d192f1aa25ebdd00" at path "_id" for model "Article"]
message: 'Cast to ObjectId failed for value "58753da5d192f1aa25ebdd00" at path "_id" for model "Article"',
name: 'CastError',
stringValue: '"58753da5d192f1aa25ebdd00"',
kind: 'ObjectId',
value: '58753da5d192f1aa25ebdd00',
path: '_id',
reason: undefined,

最佳答案

这似乎是最新版本 Mongoose 中的一个已知错误。我建议降级到 4.7.2:

https://github.com/Automattic/mongoose/issues/4867

关于javascript - Actor 错误: Cast to ObjectId failed for value "id goes here" at path "_id" for model "Article",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41578800/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com