简体   繁体   中英

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. After a user register it will route them to something like "localhost:5000/user/setting/5c27381923csw123923" (the last part is user id). Now I want to add education data into the user collection in my 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?

Try change { userid }, for {_id: userid } .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM