如何修复 TypeError: db.collections.update is not a function?

时间:2021-04-17 15:52:36

标签: javascript node.js mongodb mongoose ecmascript-6

当登录用户提交集合中的单个输入字段时,我想用集合中的两个新“键:值”对更新文档:

_id:60796d35822f7c0004647717
name:"<username>"
email:"useremail@gmail.com"
password:"$2a$10$xFerC/qbmrvY4Ue7f56TtetBroeBHgjmFgd04G/4RAk2DcT68sL1C"
date:2021-04-16T10:55:49.762+00:00
__v:0

我需要将其更新为:

_id:60796d35822f7c0004647717
name:"<username>"
email:"useremail@gmail.com"
dateInvested: <certain approved date>
investmentAmount: <certain amount>
password:"$2a$10$xFerC/qbmrvY4Ue7f56TtetBroeBHgjmFgd04G/4RAk2DcT68sL1C"
date:2021-04-16T10:55:49.762+00:00
__v:0

以下是我提交的表单的一些代码:


// Login Page to were user is asked to log back in if the submission wasn't successful
router.get("/login", forwardAuthenticated, (req, res) => res.render("login"));

// Submit
router.post("/submitAmount", (req, res) => {
    const { usersMoney } = req.body;
    let errors = [];

    if (!usersMoney) {
        errors.push({
            msg: "You need to enter some cash amount! Log back in and try again.",
        });
    } else if (usersMoney < 200) {
        errors.push({
            msg: "K200 is the minimum amount allowed Log back in and try again.",
        });
    }

    if (errors.length > 0) {
        res.render("login", { errors });
    } else {
        // Validation passed.
        const userId = req.user._id;
        db.collections.update({ _id: userId }, { $push: { invAmount: usersMoney, invDate: new Date() } });
    }
});

module.exports = router;

这是提交金额的表格:

                <form action="/users/submitAmount" method="POST">
                    <div class="form-group">
                        <label for="usersMoney">Enter Your Amount</label>
                        <input type="number" id="usersMoney" name="usersMoney" class="form-control" 
                        placeholder="Enter Your Amount" />
                    </div>
                    <button type="submit" class="btn btn-primary btn-block">
                      Submit
                    </button>
                </form>

问题是当验证通过时,我收到以下错误:

TypeError: db.collections.update is not a function
    at C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\routes\users.js:122:24
    at Layer.handle [as handle_request] (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:275:10)
    at C:\Users\KSE0002\Desktop\some-react-apps\deux_with_tm\deux\node_modules\express\lib\router\index.js:635:15

我试过跑步:

npm uninstall mongodb --save

npm install mongodb ^2.2.33

但是所有这一切加上我的其他努力都无济于事

更新

if (errors.length > 0) {
    res.render("login", { errors });
  } else {
    // Validation passed.

    const userId = req.user._id;

    User.find({ _id: userId }).then((user) => {
      if (user) {
        console.log(user);
        // This belowe is what is returned in user in my console:
==========================================================================
         {
             _id: 607849 b9d098dd2390892fd3,
             name: 'Klem Lloyd Mwenya',
             email: 'Klem.lloyd@groupkse.net',
             password: '$2a$10$NuW5.b8yrfWtN6APKz4AleqXhZUkd3N08V6PC3LgN4V6JDKvHvf9e',
             date: 2021 - 04 - 15 T14: 12: 09.224 Z,
             __v: 0
         }
==========================================================================
      }
    });
  }

我希望将其修改为以下内容:

{
             _id: 607849 b9d098dd2390892fd3,
             name: 'Klem Lloyd Mwenya',
             email: 'Klem.lloyd@groupkse.net',
             investDate: <Some date like this> (2021 - 04 - 15 T14: 12: 09.224 Z,)
             investAmount: <Some amount like this> 500
             password: '$2a$10$NuW5.b8yrfWtN6APKz4AleqXhZUkd3N08V6PC3LgN4V6JDKvHvf9e',
             date: 2021 - 04 - 15 T14: 12: 09.224 Z,
             __v: 0
         }

0 个答案:

没有答案