Javascript 为什么我的代码中会出现TypeError:User.findByIdAndUpdate(…)?梅恩栈
我正在尝试实现代码,允许用户通过更新mongoDB Atlas中的URL来更改他们的个人资料图片。以下是按下按钮应到达的路线代码: changepProfilePic.js 以下是server.js中的相关路由代码:Javascript 为什么我的代码中会出现TypeError:User.findByIdAndUpdate(…)?梅恩栈,javascript,mongoose,axios,passport.js,mern,Javascript,Mongoose,Axios,Passport.js,Mern,我正在尝试实现代码,允许用户通过更新mongoDB Atlas中的URL来更改他们的个人资料图片。以下是按下按钮应到达的路线代码: changepProfilePic.js 以下是server.js中的相关路由代码: const changeProfilePicRouter = require("./routes/changeProfilePic"); app.use("/api/changeProfilePic", changeProfilePicRou
const changeProfilePicRouter = require("./routes/changeProfilePic");
app.use("/api/changeProfilePic", changeProfilePicRouter);
以下是用户模式:
user.model.js
这是一个具有axios功能的组件,可在按下按钮时转到第一条路线:
changeProfilePic.component.js
import React,{useState}来自“React”;
从“Axios”导入Axios;
导入“bootstrap/dist/css/bootstrap.min.css”;
从“react bootstrap”导入{Button,Form,Col};
函数HookChangeProfilePic(){
const[profilePicSrc,setProfilePicSrc]=useState(“”);
constchangeprofilepic=()=>{
Axios({
方法:“放”,
数据:{
图片:profilePicSrc
},
事实上,
url:“/api/changeProfilePic”,
}).then((window.location.href=“/profile”).catch((err)=>console.log(err));
};
返回(
setProfilePicSrc(e.target.value)}
type=“text”
占位符=“输入图像Src”
/>
更新个人资料图片
);
}
导出默认的HookChangeProfilePic;
我正在使用Passport.js进行授权
对不起,如果我丢失了任何相关代码或信息!我不是想偷懒,我只是不知道什么是重要的。如果需要,我可以快速编辑和添加更多内容
我已经检查了其他问题的解决方案,这些解决方案与我的问题类似(我可以找到),但到目前为止没有一个对我有帮助。错误的全文对于发布非常重要<代码>类型错误:包括许多不同的错误类型。这可能是由于
(req、res、next)代码>下一行有悬空,但很难说。完整的错误消息会有帮助,现在有点难以确定。哦,天哪,是悬空(req,res,next);最后。我觉得自己很愚蠢。“谢谢你,”异端猴子说
const changeProfilePicRouter = require("./routes/changeProfilePic");
app.use("/api/changeProfilePic", changeProfilePicRouter);
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
googleId: {
type: String,
},
username: {
type: String,
required: true,
unique: true,
trim: true,
minlength: 3,
},
password: {
type: String,
trim: true,
},
email: String,
image: {type: String, default: 'https://i.imgur.com/Geb0OcA.png'},
date: {type: Date, default: Date.now},
tps: {type: Number, default: 0},
},
);
const User = mongoose.model("User", userSchema);
module.exports = User;
import React, { useState } from "react";
import Axios from "axios";
import "bootstrap/dist/css/bootstrap.min.css";
import { Button, Form, Col } from "react-bootstrap";
function HookChangeProfilePic() {
const [profilePicSrc, setProfilePicSrc] = useState("");
const changeProfilePic = () => {
Axios({
method: "PUT",
data: {
image: profilePicSrc
},
withCredentials: true,
url: "/api/changeProfilePic",
}).then((window.location.href = "/profile")).catch((err) => console.log(err));
};
return (
<div>
<Form className="m-0">
<Form.Row className="justify-content-md-center m-4">
<Col xs="auto">
<Form.Control
onChange={(e) => setProfilePicSrc(e.target.value)}
type="text"
placeholder="Enter Image Src"
/>
</Col>
<Button variant="warning" onClick={changeProfilePic}>
Update Profile Picture
</Button>
</Form.Row>
</Form>
</div>
);
}
export default HookChangeProfilePic;