javascript - 尝试将 $set 与 stringify 一起使用,但它不起作用

标签 javascript node.js mongodb express mongoose

我正在尝试使用此功能来编辑我的 mongodb 数据库中的一个空间,但我是新手,我不明白发生了什么。我在参数列表后收到错误 Missing ) 。谢谢大家看到这里。

app.put('/factura/:id', (req, res) => {
    res.header({'Content-Type': 'application/json'});

    var factura = req.body;
    var facturaModel = new FacturaModel({
        nro: factura.nro,
        nit: factura.nit,
        cliente: factura.cliente,
        tarjeta: factura.tarjeta,
        detalles:[{producto: factura.nombre, cantidad: factura.cantidadSeleccionada, precio_unitario: factura.precio}]
    })

    FacturaModel.findByIdAndUpdate({id: req.params.id}, (err, factura) => {
        console.log(JSON.stringify(factura));
        res.end(JSON.stringify($set: factura));
    });


});

最佳答案

JSON.stringify 接受对象作为参数。您还没有在那里创建对象,因此尝试将 $set 声明为属性是行不通的。如果您想要诸如 {"$set":"foo"} 之类的输出,则为 JSON.stringify 提供一个对象以进行 stringify:

res.end(JSON.stringify({ $set: factura }));

关于javascript - 尝试将 $set 与 stringify 一起使用,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50092849/

相关文章:

javascript - 根据设备宽度向面包屑添加尾随省略号

javascript - 将数据传递给其他组件的最佳方式是什么

javascript - 遍历 Hogan.js 中的键/值

python - 从用 Node.js 编写的 AWS Lambda 函数调用 Python boto3 库

node.js - Mongoose - 在 Model.create 中选择特定字段

mongodb - 仅通过有效负载更新

c++ - mongo-cxx-driver 安装会安装库

javascript - Canvas - 适合慢速网络的游戏加载

sql - 在 postgres 中使用 upsert 和 nodejs 中的 json 文件

c# - MongoDB.Driver C#自动过期文件