I have a test charge coming back as desired, but then when I try to execute a save to the database, mongoose.save simply doesn’t run:
stripe.charges.create({
amount : 2000,
currency : "usd",
source : "tok_visa",
receipt_email : req.body.email,
}).then((charge) => {
console.log(charge);
console.log("line 54");
// create a new document for the donor, donation, email
const new_donor = new Donor({
first_name : req.body.first_name,
last_name : req.body.last_name,
email : req.body.email,
});
console.log("line 63");
new_donor.save((error, document) => {
console.log("line 65");
if (error) {
console.log(error);
} else {
console.log("line 69");
const new_donation = new Donation({
donor_id : document._id,
donation_amount : parsed_amount
});
the log statement on line 63 shows in console. But not line 65.
Does anything stick out as an issue? I thought I had promises and saving to the DB down.
No error? Did you log new_donor
?
No error, I added new_donor as a 2nd argument to the line 63 log statement and got back this:
<a big charge object here>
line 54
line 63, new donor: { _id: 5be1ffb010991b07f0ebd059,
first_name: 'rsrrt',
last_name: 'dsstdsd',
email: 'rstrst@strs.com' }
Ok, here is donor.js:
const mongoose = require("mongoose");
const DonorSchema = new mongoose.Schema({
first_name : {
type : String,
maxLength: 15
},
last_name : {
type : String,
maxLength: 25
},
email : {
type : String,
maxLength: 40
},
card_number : { // hash and salt cc info. Or don't add it
type : String,
length : 16,
},
card_expiration : {
type : String,
maxLength: 6
}
});
const DonorModel = mongoose.model("donation_checkout_donor", DonorSchema);
module.exports = DonorModel;
PS: I don’t think I’ll be adding the CC info to the db
I can’t see anything wrong. I’ve found this stack overflow question and it looks like a lot of subtle things that may have not to do with this code at all is wrong.
Please go through each, if you can’t find the solution there we’ll keep trying.
1 Like
Thank you for that. I will look into it. I am copying all of index.js below in case it helps
require("dotenv").load();
var express = require('express');
var router = express.Router();
const Donor = require('../models/donor');
const Donation = require('../models/donation');
const Email = require("../models/email");
const stripe = require('stripe')(process.env.STRIPE_TEST_SECRET_KEY);
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/donate1', (req, res, next) => {
res.render('donate1');
});
router.post('/api/donate', (req, res, next) => {
let parsed_amount = 0; // amount of the charge
if (req.body.step === '1') {
console.log("step 1 end");
// steps
res.cookie('amount', req.body.donation_amount, {
// maxAge : 3600000,
httpOnly : true
});
for (i = 0; i < req.body.donation_amount.length; i ++) {
console.log(i, parseInt(req.body.donation_amount[i]));
parsed_amount = parsed_amount + (parseInt(req.body.donation_amount[i]) || 0); // OR returns 1st if true, otherwise 2nd
}
console.log("end of loop", parsed_amount);
res.render('donate2', { amount : parsed_amount });
} else if (req.body.step === '2') {
console.log("step 2 end");
// grab all the data
// ship it to Stripe
stripe.charges.create({
amount : 2000,
currency : "usd",
source : "tok_visa",
receipt_email : req.body.email,
}).then((charge) => {
console.log(charge);
console.log("line 54");
// create a new document for the donor, donation, email
const new_donor = new Donor({
first_name : req.body.first_name,
last_name : req.body.last_name,
email : req.body.email,
});
console.log("line 63, new donor: ", new_donor);
new_donor.save((error, document) => {
console.log("line 65");
if (error) {
console.log(error);
} else {
console.log("line 69");
const new_donation = new Donation({
donor_id : document._id,
donation_amount : parsed_amount
});
new_donation.save((error, document) => {
if (error) { console.log(error) }
});
const new_email = new Email({
donor_id : document._id,
is_referred : false,
first_name : req.body.first_name,
email : req.body.email
});
new_email.save(error => {
if (error) { console.log('error'); }
})
}
});
}
)
.catch(e => console.log(e));
res.render('donate3');
} else if (req.body.step === '3') {
// steps
res.clearCookie();
res.render('donate4');
}
});
module.exports = router;
Where is your mongoose connection?
1 Like
SeanDez
November 6, 2018, 9:22pm
10
Argh, that must be it Thank you
ghukahr
November 6, 2018, 9:25pm
11
No problem, mongoose definitely should give us an error in this case
1 Like