简体   繁体   English

节点 js 和 mongoose 无效 arg 类型错误

[英]node js and mongoose invalid arg type error

I am trying to find a user by their username using parameters.我正在尝试使用参数通过用户名查找用户。 Here is my server side.这是我的服务器端。

const express = require("express");
const mongoose = require("mongoose");
const app = express();

//ROUTES
const userRoute =require("./src/routes/userRoutes");
const postRoute =require("./src/routes/postRoutes");
const commentRoute =require("./src/routes/commentRoutes");

const port = process.env.PORT || 5000;

mongoose.connect("mongodb://localhost:27017/projectDB", {useNewUrlParser: true});

app.use(express.json());

app.use(express.urlencoded());

app.get("/", (req, res) =>{
    res.send("hello");
});

app.use("/", userRoute, postRoute, commentRoute);


app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

here is my routing code for user.这是我的用户路由代码。

const express = require('express');
const userRouter = express.Router();

const userController = require("../controllers/userController");


userRouter.route("/users/:userName")
.get(userController.getUser)
.patch(userController.updateUser)
.delete(userController.deleteUser)
.post(userController.newUser);

module.exports = userRouter;

and here is the controller for the user.这是用户的 controller。 All very simple.一切都非常简单。

    const mongoose = require("mongoose");
    const Users = require("../models/userModel");
    
    module.exports.getUser = (req, res) => {
        console.log(req.params.userName);
        res.status(200).send(Users.find({name: req.params.userName}), (err, res)=>{
            if (res.err){
                console.log("USER NOT FOUND");
            }else{
                console.log("Success!");
            }
        });
    };

when I try a get request to route "..../users/gokdeniz" which is a valid user in the database, I get an error saying当我尝试获取请求以路由“..../users/gokdeniz”这是数据库中的有效用户时,我收到一条错误消息

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received type function ([Function (anonymous)])
    at new NodeError (node:internal/errors:371:5)
    at Function.from (node:buffer:322:9)
    at ServerResponse.send (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\response.js:193:22)
    at module.exports.getUser (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\src\controllers\userController.js:6:21)
    at Layer.handle [as handle_request] (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\route.js:144:13)      
    at Route.dispatch (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\index.js:284:15
    at param (C:\Users\tlege\OneDrive\Masaüstü\project 1\untitled_project\Server\node_modules\express\lib\router\index.js:365:14)

Find user beforehand and then pass the data you want to the send() function.事先找到用户,然后将您想要的数据传递给 send() function。

  module.exports.getUser = async (req, res) => {
            const user = await Users.find({name: req.params.userName});
            if (user) {
               res.status(200).send(user)
            } else {
               res.status(404).send({error: "Not found"})
            }
           
  };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node js猫鼬类型错误 - Node js Mongoose Type Error TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型 Node.JS - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string Node.JS 错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 尝试 node utils/nftport/uploadFiles.js 时收到 undefined - error:TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined when trying node utils/nftport/uploadFiles.js 在 node.js 中使用 request-promise 模块并获取 http_outgoing.js:618 throw new ERR_INVALID_ARG_TYPE('first argument', error - post using request-promise module in node.js and getting http_outgoing.js:618 throw new ERR_INVALID_ARG_TYPE('first argument', error ERR_INVALID_ARG_TYPE错误 - ERR_INVALID_ARG_TYPE error Testcafe 抛出错误无效的 arg 类型 - Testcafe throws an error invalid arg type Node.js 应用程序类型错误 [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型对象 - Node.js app TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object Node.js:[ERR_INVALID_ARG_TYPE]:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。 收到未定义 - Node.js: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined 节点管道的类型错误 [ERR_INVALID_ARG_TYPE] - TypeError [ERR_INVALID_ARG_TYPE] with node pipeline Discord.js 上的 ERR_INVALID_ARG_TYPE - ERR_INVALID_ARG_TYPE on Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM