Javascript 如何修复TypeError:db.collections.update不是一个函数?
当登录用户提交集合中的单个输入字段时,我想在集合中使用两个新的“key:value”对更新文档,该字段如下所示:Javascript 如何修复TypeError:db.collections.update不是一个函数?,javascript,node.js,mongodb,mongoose,ecmascript-6,Javascript,Node.js,Mongodb,Mongoose,Ecmascript 6,当登录用户提交集合中的单个输入字段时,我想在集合中使用两个新的“key:value”对更新文档,该字段如下所示: _id:60796d35822f7c0004647717 name:"<username>" email:"useremail@gmail.com" password:"$2a$10$xFerC/qbmrvY4Ue7f56TtetBroeBHgjmFgd04G/4RAk2DcT68sL1C" date:2021-
_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
我试过跑步:
但这一切加上我的其他努力都无济于事
已更新
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
}
{
_识别号:607849 b9d098dd2390892fd3,
名称:“Klem Lloyd Mwenya”,
电子邮件:“克莱姆。lloyd@groupkse.net',
投资日期:(2021-04-15T14:12:09.224)
投资额:500
密码:“$2a$10$NuW5.b8yrfWtN6APKz4AleqXhZUkd3N08V6PC3LgN4V6JDKvHvf9e”,
日期:2021-04-15T14:12:09.224Z,
__v:0
}
您缺少一些需要检查的基本代码。db
来自哪里?你的收藏名是什么?你的猫鼬模型在哪里?看起来您正试图将MongoDB shell查询直接移植到JS中,这根本不起作用。你读过Mongoose文档了吗?你是对的,我没有为这个动作做模型或模式。我所做的唯一模式是在注册时。只是因为我不知道怎么做。如何编写只将这两个键值对添加到集合中各自文档的代码?
<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
}