簡體   English   中英

節點中的 find 方法拒絕從 mongodb 中的集合返回所有數據

[英]The find method in node refuses to return all data from collection in mongodb

我正在為 twitter 克隆創建后端,每條路線都運行良好,但是當我嘗試讓所有用戶提供他們的信息時遇到錯誤,代碼如下

router.get("/tweets", (req, res) => {
    User.find()
        .then((user) => res.json(user))
        .catch((err) => res.json({ error: "an error occured", err }));
});

這是我在 postman 中發出獲取請求后一直運行的錯誤


(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.

有趣的是,我使用同樣的方法在另一個代碼中返回所有用戶並且它有效,所以我不知道是不是因為我遇到這個問題的模式的長度。

這是 model

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) => {
    ////////
});

但它高於問題中的前一條路線,所以在我選擇這條路線上方的路線后,它在到達底部之前得到了匹配並解決了問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM