findOneandUpdate is not defined

Trying to do a put request to update data. I am having trouble getting access to the data in the request object or should I say getting the form field values into the request object.

here is the function thats trying to perform the fetch:

function EditComment(e){
    const input= document.getElementById('editInputs')
    e.preventDefault();
    console.log('inside EditCOmment the id is :'+newId)
        fetch('/update/' + newId, {
      method: 'Put',
       headers : {
        "Content-Type" : "application/json; charset=utf-8" 
    },   
    body : JSON.stringify({todo : input.value()})
    })
      .then(response => response.json())
      .then(function (response) {
        if (response.status === 'success') {
      
         console.log(response)
        }
      })
      .catch(err => {
        console.log('fetch update didn\'t succeed\n' + err);
      });
  }

here is the form its trying to get the data from:

                   <form id="editcommentForm" method="put" /*action="/update"*/ >
                        <div class="form-group">
                          <label>Name</label><input type="text" class='form-control' name="client-name" placeholder="Enter name" required>
                        </div>
                        <div class="form-group">
                          <label>Comments</label><textarea id="editInputs" class="form-control" name="comment" placeholder="Enter Comments "rows="3"></textarea>
                        </div>
                        
                        <button id="edit-btn" type="submit" class="btn btn-primary btn-lg btn-block">Submit Data</button>
                      </form>

and here is my route:

const updateComment = (req, res) => {
  const userInput= req.body;
  console.log(userInput);
  console.log(req.params);
  console.log('***********************************')
  findOneAndUpdate({_id: req.params.id},{$set: {name: userInput['client-name'], comment: userInput.comment}},{new: true}, function(err, data){
    if (err) {
      res.send({
        status: 'failure',
        error: err
      });
      return;
    }
    res.json({
      comments,
      status: 'success'
    });
  })
};

I am getting findOneAndUpdate is not defined. And the req.body is completely empty, what am I doing wrong?

I am trying to find a record by Id, and update its values.
Edit: and Im gettting a typeError in the browser console: input.value is not a function

‘findOneAndUpdate()’ is a method… on what Object? What thing are you trying to update?

I already know, that was your hint.:wink:

1 Like

I am trying to set the fields of the feedbacks object that is stored in the database I thought the above line of code does that isnt the second arguement passed to findoneandupdate supposed to the the object and how you want it to be updated?
Even so, I am not seeing anything in the request object thats being sent back I would expect to see that filled with the form data but whats being output in the node console is an empty object?

here is my code in model

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const feedbackSchema = new Schema({
  name: {
    type: String,
    required: 'enter a name'
  },
  email: {
    type: String,
    required: 'enter an email'
  },
  comment: {
    type: String
  }
});

module.exports = { feedbackSchema };

Thanks for reviewing my code.
Edit: I want to update the record in the database by id that I am taking from the params with the data that is sent in the request via the form fields. This data needs to be in the req.body which is why ive told you its empty… (I believe that the first arg passed to findONeAndUpdate is correct bc I see it logging in NOde)

also Is the above necessary at all? in the json.stringify i dont understand the key. can the update succeed if I delete the above lines?
also the editinput variable is only referring to one formfield but I want to also edit name. these are the things I am expecting to be in the req.body

hi kravmaguy
findOneAndUpdate is a method of a mongoose model
rather then module.exports = { feedbackSchema };
you have to export your model
mongoose.model(‘Feedback’, feedbackSchema);
and in your router
const Feedback = mongoose.model(‘Feedback’)
Feedback.findOneAndUpdate(your query)

2 Likes

value is a property not a function input.value, but you are calling it like a function.

body : JSON.stringify({todo : input.value()})

Docs for findOneAndUpdate

1 Like