node.js - 如何将数据库中价格的字符串格式更改为 mongodb 中的整数格式?
问题描述
这是我的输入表单。在这里,价格标签在Database中保存为字符串。如何将其更改为整数格式。我正在使用 mongodb 3.2 版本
<section>
<div class="container mt-4">
<div class="row">
<div class="col-md-6">
<h2 class="text-center">Add Product</h2>
<form action="/admin/add-product" method="POST" enctype="multipart/form-data">
<label for="">Name</label>
<input type="text" name="Name" class="form-control">
<label for="">Category</label>
<input type="text" name="Category" class="form-control">
<label for="">Price</label>
<input type="number" name="Price" class="form-control">
<label for="">Description</label>
<input type="text" name="Description" class="form-control">
<label for="">Image</label>
<input type="file" name="Image" class="form-control">
<button type="submit" class="btn btn-success mt-4">Submit</button>
</form>
</div>
</div>
</div>
</section>
添加到购物车
addProduct:(product,callback)=>{
db.get().collection("product").insertOne(product).then((data)=>{
console.log(data)
callback(data.ops[0]._id)
})
}
解决方案
在将产品添加到数据库之前将价格更改为整数
let Price=parseInt(req.body.Price)
req.body.Price=Price
然后将产品添加到 mongodb
推荐阅读
- firebase - Firestore Web 界面不允许创建阵列
- node.js - 如何在 Node.js 中从 Websocket 接收二进制数据?
- c# - 在没有位图的 C# 面板上绘制不止一件事
- php - 这种创建“真正”随机字符串的方式有意义吗?
- arrays - 在工作表数组中的工作簿之间复制和粘贴数据
- multithreading - 哪个在多核系统上有效 - 具有多个线程的进程或具有单个线程的多个进程?
- unity3d - 如何创建可折叠的汉堡菜单图标自定义检查器/编辑器 Unity
- scala - 在普通的 sql 平滑查询中使用 `TableQuery[A]`
- c++ - avl 树 - 将旋转问题
- websocket - MediaSource 缓冲区仅适用于一个客户端