[英]insert new data into mongodb under dynamic route using mongoose
I have a form that add additional information for a specific user, my schema has, name, password, email, and education and education data wont be asked before user created an account first.我有一个为特定用户添加附加信息的表单,我的架构有名称、密码、email,并且在用户首先创建帐户之前不会询问教育和教育数据。 After a user register it will route them to something like "localhost:5000/user/setting/5c27381923csw123923" (the last part is user id).用户注册后,它会将他们路由到类似“localhost:5000/user/setting/5c27381923csw123923”的位置(最后一部分是用户 ID)。 Now I want to add education data into the user collection in my mongodb.现在我想将教育数据添加到我的 mongodb 中的用户集合中。
form class="info" action="/users/setting" method="POST">
<div class="fields">
<div class="education">
<label for="education">Education</label>
<select name="education" id="education">
<option value="bachelor">bachelor</option>
<option value="Master">Master</option>
<option value="Phd">Phd</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<button type="submit" class="signin-button">Submit</button>
</form>
My post route looks like this:我的发布路线如下所示:
router.post("/setting/:id", (req, res, next) => {
const userid = req.params.id;
const { education } = req.body;
User.updateOne(
{ userid },
{ $set: { education } },
(err) => {
if (err) {
throw err;
} else {
res.send("good");
}
}
);
});
When I click the "submit" button nothing is being changed or inserted.当我单击“提交”按钮时,没有任何内容被更改或插入。 What should I do when the route is dynamic like "localhost: 3000/user/setting/5c2312327318273s" and try to insert data for that specific user id?当路由是动态的,如“localhost:3000/user/setting/5c2312327318273s”并尝试为该特定用户 ID 插入数据时,我该怎么办?
Try change { userid },
for {_id: userid }
.尝试将{ userid },
更改为{_id: userid }
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.