首页 > 解决方案 > TypeError:更新文档需要原子操作符

问题描述

错误详情:

TypeError: Update document requires atomic operators
    at new UpdateOneOperation (C:\Users\niko\Desktop\My Workspace\node_modules\mongodb\lib\operations\update_one.js:14:13)
    at Collection.updateOne (C:\Users\niko\Desktop\My Workspace\node_modules\mongodb\lib\collection.js:772:5)      
    at exports.insert (C:\Users\niko\Desktop\My Workspace\controllers\controller.js:16:47)
    at Layer.handle [as handle_request] (C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\index.js:335:12)    at next (C:\Users\niko\Desktop\My Workspace\node_modules\express\lib\router\index.js:275:10)
const {getDb} = require('../config/db-steup')

exports.insert = (async (req,res) => {
    const object = {
        financial: {
            salary: {
                amount: req.body.financial.salary.amount,
                type: req.body.financial.salary.type,
            }
        }
    }

    const name = req.body.name;

    try{
        await getDb().db().collection('post').updateOne({$push:{ posts: object}}, {name:name});
        return res.json({message: "Sucessfully Updated DB"})
    }
    catch (err){
        console.log(err)
    }
});


exports.delete = (async (req, res, next) => {
    var {email} = req.query;

    console.log(email);
    
    const emailExist = await getDb().db().collection('test').findOne({email});
    if (emailExist) {
        await getDb().db().collection('test').deleteOne({email})
        return res.json({message: "User Successfully deleted"})
    }else{
        return res.json({message: "email doesnt exist"})
   } 
});

标签: javascriptnode.jsmongodbexpress

解决方案


推荐阅读