Reputation:
i have a http put function in ReactJs that looks like this
UpdateItem: function(_id) {
var input = this.refs.myInput;
$.ajax({
url: 'http://localhost:3000/update/' + _id,
type: 'PUT',
data:input,
cache: false,
success: function(result) {
console.log(data);
window.location.reload();
}
});
}
it takes an input and it should send the new value with the request but the it never does
<input ref="myInput" type="number" name="quantity" min="1" max="10" defaultValue={item.quantity} className="form-control"/>
when i look at the console the req.body shows this [object Object] and my put function in node looks like this
app.put('/update/:_id', function(req, res) {
console.log(req.params);
console.log("your req is" +req.body);
Cart.findByIdAndUpdate(req.params.id, { quantity: req.body.quantity }, function(err, product) {
if (err) throw err;
console.log(product);
console.log(product);
});
});
any ideas of what the problem could be ?
Upvotes: 2
Views: 2611
Reputation: 59511
This is happening because you are trying to concatenate a string with an object. That's what the +
-sign does in your console.log
.
You'll notice that console.log(req.body)
and console.log("your req is")
both work individually, but console.log("your req is" + req.body)
will give you the wrong output.
To fix this, either do two separate outputs, or use this:
console.log("your req is", req.body);
Now you'll get both the string and the Object properties to output in the console.
(check the output in the console)
var person = {name: "Bob", sirname: "Smith", age: 34};
console.log("The person data is: ", person); //right
console.log("The person data is: " + person); //wrong
Upvotes: 3
Reputation: 23049
It just saying that your "req.body" is some kind of Object, which it should be.
If you want to see what is inside, this can be handy :
console.log(JSON.stringify(req.body))
Upvotes: 0