【问题标题】:How can I add/subtract data from a EJS template如何从 EJS 模板中添加/减去数据
【发布时间】:2018-03-25 02:31:02
【问题描述】:

我不知道如何从 EJS 模板中减去数据。这是我的架构:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var InventorySchema = Schema({
    name: [{type: String, required: true, unique: true}],
    quantity: [Number],
    code: [String],
    price: [Number],
    stock: [{type: Boolean, default: true}]
})

var Inventory = mongoose.model("Inventory", InventorySchema);

module.exports = Inventory;

以下是显示我所有数据的主要查询:

router.get("/", (req, res)=>{
    InventoryModel.find({})
    .then((inventory)=>{
        console.log(inventory);
        res.render("inventory/index", {inventory: inventory})
    })
    .catch((err)=>{
        console.log(err);
    })
})

下面是我要从中添加/减去数据的表单:

<div class="container">
    <form action="/inventory" method="POST">
    <label>Name</label>
    <input type="text" name="itemName" placeholder="item name"><p/>
    <label>Quantity</label>
    <input type="number" name="quantity" placeholder="quantity"><p/>
    <button>Create</button>
    </form>
</div>

这就是我想要完成的任务。无论我在数量字段中输入什么数字,只要我点击创建按钮(这是一个 POST),就从我的 mongodb 数据库中减去该数据

有什么帮助吗?

谢谢

【问题讨论】:

  • 你想让数量成为一个数字数组,不就是一个数字吗?

标签: node.js mongoose


【解决方案1】:

您需要更新现有文档并使用$inc 运算符。

Example:
# create query conditions and update variables
const conditions = { },
    update = { $inc: { views: 1 }}; # inc accepts negative numbers

# update documents matching condition
Model.update(conditions, update)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 2011-01-16
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多