node.js - 节点中的find方法拒绝从mongodb的集合中返回所有数据
问题描述
我正在为 twitter 克隆创建一个后端,每条路由都运行良好,但是当我尝试让所有用户获取他们的信息时遇到错误,代码如下
router.get("/tweets", (req, res) => {
User.find()
.then((user) => res.json(user))
.catch((err) => res.json({ error: "an error occured", err }));
});
这是我在邮递员中发出获取请求后一直运行的错误
(node:16836) UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "tweets" at path "_id" for model "User"
at model.Query.exec (C:\Users\hp\Documents\P. Languages\React\MERN\pro\client\node_modules\mongoose\lib\query.js:4351:21)
at model.Query.Query.then (C:\Users\hp\Documents\P. Languages\React\MERN\pro\client\node_modules\mongoose\lib\query.js:4443:15)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16836) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16836) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
有趣的是,我使用同样的方法在另一个代码中返回所有用户并且它有效,所以我不知道是不是因为我遇到这个问题的模式的长度。
这是模型
const mongoose = require("mongoose");
const userSchema = mongoose.Schema({
firstName: {
type: String,
required: true,
min: 2,
max: 255,
},
lastName: {
type: String,
required: true,
min: 2,
max: 255,
},
email: {
type: String,
required: true,
min: 2,
max: 255,
},
password: {
type: String,
required: true,
min: 2,
max: 255,
},
tweets: [
{
tweet: {
type: String,
createIndex: true,
unique: true,
date: Date.now,
},
},
],
tweetNo: {
type: Number,
required: true,
},
followers: [
{
default: [],
},
],
following: [
{
default: [],
},
],
numFollowers: {
type: Number,
default: 0,
},
numFollowing: {
type: Number,
default: 0,
},
});
userSchema.followers = [
{
type: userSchema,
},
];
userSchema.following = [
{
type: userSchema,
},
];
module.exports = mongoose.model("User", userSchema);
这是来自 Mongo db 的示例数据
_id:5f03c0f2c4cb6f43749fdcde
numFollowers:0
numFollowing:0
firstName:"Babagana"
lastName:"Abba"
email:"babagabba@gmail.com"
password:"$2b$10$3csAMnCf/3fRL5rwr9koMeaSkFBvZ2T5noiHG0QhI5XZHFOUjGHOm"
tweets:Array
0:Object
_id:5f03c0f2c4cb6f43749fdcdf
tweet:"my name is Babagana"
tweetNo:1
followers:Array
following:Array
__v:1
解决方案
我有一条 id 像这样的路线
router.get("/:id", async (req, res) => {
////////
});
但它高于问题中的前一条路线,所以在我选择这条路线上方的路线后,它在到达底部之前得到了匹配并解决了问题
推荐阅读
- c# - 如何在没有 azure 的情况下发布和部署多渠道机器人?
- fast-ai - Fastai 2020 课程 v4 windows 设置
- laravel - 如何使用多个 where 必须过滤 morphedByMany 关系?
- html - 创建一个不连接到任何其他边框的边框
- ruby-on-rails - Rails 6.1 参数编码设置为 ASCII-8BIT
- facebook - facebook messenger curl 请求返回
- docusignapi - Docusign 文档 URL 链接到空白模板
- php - 使用 PHP 集成 oauth 访问令牌
- reactjs - 为什么AsyncStorage在第一次单击时保存一个空数组,然后仅在第二次单击时保存输入的值?
- angular - 从 json 角度返回对象对象中获取数据