0

This error appears in my node js console:

(node:6048) UnhandledPromiseRejectionWarning: MongoError: document must be a valid JavaScript object

router.post('/datapassfup', (req, res) => {
        console.log("submitted values are",req.body)
        MongoClient.connect(url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mohan");
            var myobj =req.body;
            dbo.collection("customers").updateOne({$set:{myobj}}, function(err, res) {
              if (err) throw err;
              //console.log("1 document inserted");
              db.close();
            });
          });
        res.json({
            statusCode: 200,
            result: "hi how are you!",
        })
    }
    );
2
  • data can't be update in mongodb database...Give any solution Commented Jul 16, 2019 at 7:24
  • What do you mean by data can't be update in mongo database? Are you checking the right table? Because the updated object returned by node.js is snapshot of new data on database side. Commented Jul 16, 2019 at 10:31

2 Answers 2

1

updateOne() has first parameter as filter. Define your filter and then update.

//suppose you are getting id in order to update
    let userId = req.body.updatedUser.id;
    let userName = req.body.updatedUser.username;
    let name = req.body.updatedUser.name;
    dbo.collection("customers").updateOne({_id: userId},{$set:{name, username:userName}}, 
    function(err, res) {
                  if (err) throw err;
                  //console.log("1 document inserted");
                  db.close();
                });
              });
Sign up to request clarification or add additional context in comments.

12 Comments

This type of code I can get an updated value in node js console but it does not update on mongodb. I could not find the reason. Please help me
Can you write the object that you are getting in req.body ? Furthermore, have you confirmed that you are connecting to DB?
updated values are { updatedUser: { id: 1, name: 'hello', username: 'Raja Gokul' } }--this is what am getting in node js
Yes its connected to DB
So you are getting all values inside "updatedUser" object. If this is what you are getting, i updated my answer accordingly. Just make sure about the id field. _id in MongoDB stores in the form of hexadecimal. Since you are getting id as 1, so i don't think so you want to compare it with _id in MongoDB. apart from _id, do you have id field in MongoDB as well? if so replace _id with id.
|
0

I think you were not passing the condition that where you want the update like add name=mohan .

Use cors for cross origin request and body parser for parse the form data

const cors = require("cors");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser());
app.use(bodyParser.urlencoded());
app.use(cors());
var MongoClient = require('mongodb').MongoClient;
//url like var url = "mongodb://localhost:27017"
     router.post('/datapassfup', (req, res) => {
                console.log("submitted values are",req.body)
                MongoClient.connect(url, function(err, db) {
                    if (err) throw err;
                    var dbo = db.db("mohan");
                    var myobj =req.body;
                    dbo.collection("customers").updateOne({name:"mohan"},{$set:{myobj}}, function(err, res) {
                      if (err) throw err;
                      //console.log("1 document inserted");
                      db.close();
                    });
                  });
                res.json({
                    statusCode: 200,
                    result: "hi how are you!",
                })
            }
            );

4 Comments

This type of code I can get an updated value in node js console but it does not update on mongodb. I could not find the reason. Please help me
check the data is came with request or not ?
Am fresher so I do not know exactly how to do this so give me some examples crud application using React js with mongoDB and nodeJS
now i have add more details if this answer solve your problem make accept it as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.