I am trying to update a data in mongodb using nodejs. I want the total data to be updated by +1 once a user creates a transaction. But I don't have any idea of it. Because there is no value coming back. like req.body, that I can pass in.
var UserSchema = new mongoose.Schema({
total: { type: Number, default: 0 }
});
UserSchema.plugin(passortLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
app.post("/bitcoin", isLoggedIn, function(req, res) {
client.createTransaction({ currency1: "USD", currency2: "BTC", amount: 500 },
function(err, result) {
if (err) {
console.log(err);
} else {
User.findByIdAndUpdate(req.params.id, { total: +1 }, function(error,updated) {
if (error) {
console.log("error occured " + error);
return res.redirect("/dashboard");
} else {
console.log("total updated" + updated);
}
});
var coinPayment = result;
res.redirect(coinPayment.status_url);
}
}
);
});
It console.logs this below and it does not update any work around for this
total updated null
req.params.idis not in the collection. So thefindByIdAndUpdatefind nothing.