首页 > 解决方案 > 节点中的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

标签: node.jsmongodbexpressmongoosemongoose-schema

解决方案


我有一条 id 像这样的路线

router.get("/:id", async (req, res) => {
    ////////
});

但它高于问题中的前一条路线,所以在我选择这条路线上方的路线后,它在到达底部之前得到了匹配并解决了问题


推荐阅读