我真的很想如何将getAVPPlayerInfo()的结果返回到调用它的位置(从这一行的generatePlayersArray函数:var avpinfo = getAVPPlayerInfo(pinfo);
在此之前,我一直在使用没有generatePlayersArray()和getAVPPlayerInfo()的代码,但是播放器数据之前不需要进行更新。我见过人们将行更改为var avpinfo = await getAVPPlayerInfo(pinfo);
,但这不能解决问题。
如何更改此设置以解决这两个功能之间的断开连接?
// creates an order with all cart items and user info
var userNewOrder = function(charge, cart, callback) {
console.log('Saving customer ORDER for charge', charge.id);
Order.create({
cart: {
username: cart.username,
items: cart.items,
totalQty: cart.totalQty,
totalPrice: cart.totalPrice
},
customer: cart.customerID,
paymentID: charge.id,
createdOn: new Date(),
paymentStatus: 'PAID',
user: cart.username
})
.then(function(order) {
console.log('Order created!', order._id);
saveUserRegistrations(order, callback);
})
.catch(function(err) {
console.log('Error creating order', err);
callback(err);
});
};
// manipulate the data to prepare for registration data
var saveUserRegistrations = function(order, callback) {
console.log('Saving registrations from order', order.id);
//put cart items into array of objects
var registrations = generateRegistrationsArray(order);
// console.log("Registrations ",registrations);
var paymentId = order.paymentID;
var userobject = order.user;
// add paymentid to arrays
for (var i in registrations) {
registrations[i].paymentID = paymentId;
registrations[i].users = userobject;
for (var x in registrations[i].players) {
console.log(registrations[i].players[x]);
}
}
console.log(
'Registrations with payment ID and username added',
registrations
);
saveRegistrations(registrations, callback);
};
// generates the registrations array from the cart items
var generateRegistrationsArray = function(order) {
console.log('Generating array from cart items');
var arr = [];
for (var i = 0; i < order.cart.items.length; i++) {
var players = generatePlayersArray(order.cart.items[i]);
console.log('Players: ', players);
// create registration, using new players array
arr.push({
event: order.cart.items[i].event,
field: order.cart.items[i].field,
day: order.cart.items[i].day,
division: order.cart.items[i].division,
level: order.cart.items[i].level,
group: order.cart.items[i].group,
numplayers: order.cart.items[i].numberofplayers,
price: order.cart.items[i].price,
players: players,
createdOn: new Date(),
net: null,
team: null,
notes: null,
paymentNote: null,
waiversSent: false,
reviewed: false,
active: true
});
console.log('Registrations:', arr);
}
console.log('Registrations array', JSON.stringify(arr));
return arr;
};
// generates the player array from the cart and AVP info
var generatePlayersArray = function(items) {
console.log('Generating players array from cart items');
var parr = [];
for (var i = 0; i < items.players.length; i++) {
var pinfo = {
last: items.players[i].last,
avp_id: items.players[i].avp_id
};
var avpinfo = getAVPPlayerInfo(pinfo);
console.log('AVP info returned', avpinfo);
// create registration, using new players array
parr.push({
first: avpinfo.first || items.players[i].first,
last: avpinfo.last || items.players[i].last,
email: items.players[i].email,
address: avpinfo.address || items.players[i].address,
city: avpinfo.city || items.players[i].city,
state: avpinfo.state || items.players[i].state,
zip: avpinfo.zip || items.players[i].zip,
country: items.players[i].country,
phone: avpinfo.phone || items.players[i].phone,
signed: false,
sandbagger: false,
waivers: [],
createdOn: new Date(),
adult: avpinfo.adult || items.players[i].adult,
avp_id: items.players[i].avp_id,
ranking: items.players[i].ranking,
overallRanking: items.players[i].overallRanking,
shirt_size: items.players[i].shirtSize
});
console.log(parr);
}
console.log('Players array', JSON.stringify(parr));
return parr;
};
// get AVP data to update player info
var getAVPPlayerInfo = function(pinfo) {
console.log('Admin AVP API begin', pinfo);
avp_id = pinfo.avp_id;
last = pinfo.last;
// GET THE AVP DATA
var options = {
uri: `http://api.volleyamerica.com/VAREST.svc/V6rHHdPO/players/${avp_id}`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
rp(options)
.then(function(resp) {
console.log('Player has data', resp);
let avp = {};
let address = resp.Address1 || null;
if (resp.Address2) {
address += ` ${resp.Address2}`;
}
// GET AVP member data (shirt size and ranking for db)
avp.avp_id = avp_id;
avp.shirtSize = resp.AdultShirtSize;
avp.first = resp.FirstName;
avp.last = resp.LastName;
avp.email = resp.Email || null;
avp.phone = resp.HomePhone || null;
avp.address = address || null;
avp.signed = false;
avp.sandbagger = false;
avp.waivers = [];
avp.city = resp.City || null;
avp.state = resp.State || null;
avp.zip = resp.Zip || null;
// get overallRankings greater than 0
avp.overallRanking = _.pickBy(
_.get(resp, 'OverallRanking', {}),
(ranking, key) => {
return ranking > 0;
}
);
// get rankings greater than 0
avp.ranking = _.pickBy(_.get(resp, 'Ranking', {}), (ranking, key) => {
return ranking > 0;
});
let adult = null;
// if player is under 18
const birthday = moment(resp.Birthdate, 'M/D/YYYY');
age = moment().diff(birthday, 'years', false);
if (age < 18) {
avp.adult = false;
} else {
avp.adult = true;
}
console.log('AVP API SUCCESS, returning avp data ', avp);
return avp;
})
.catch(function(err) {
console.log('AVP data error', err);
});
};
//save registrations to the database
var saveRegistrations = function(registrations, callback) {
console.log('SAVING REGISTRATIONS TO DB', registrations);
// var reginfo = registrations;
Registration.collection
.insert(registrations)
.then(function(r) {
console.log('Successfully saved registrations!', r.insertedCount);
nodemailerConfirmation(registrations, callback);
})
.catch(function(err) {
console.log(err);
for (var i in err.writeErrors) {
console.log(err.writeErrors[i].errmsg);
}
callback(err.message);
});
};
答案 0 :(得分:0)
// creates an order with all cart items and user info
var userNewOrder = function(charge, cart, callback) {
console.log('Saving customer ORDER for charge', charge.id);
Order.create({
cart: {
username: cart.username,
items: cart.items,
totalQty: cart.totalQty,
totalPrice: cart.totalPrice
},
customer: cart.customerID,
paymentID: charge.id,
createdOn: new Date(),
paymentStatus: 'PAID',
user: cart.username
})
.then(function(order) {
console.log('Order created!', order._id);
saveUserRegistrations(order, callback);
})
.catch(function(err) {
console.log('Error creating order', err);
callback(err);
});
};
// manipulate the data to prepare for registration data
var saveUserRegistrations = async function(order, callback) {
console.log('Saving registrations from order', order.id);
//put cart items into array of objects
var registrations = await generateRegistrationsArray(order);
// console.log("Registrations ",registrations);
var paymentId = order.paymentID;
var userobject = order.user;
// add paymentid to arrays
for (var i in registrations) {
registrations[i].paymentID = paymentId;
registrations[i].users = userobject;
for (var x in registrations[i].players) {
console.log(registrations[i].players[x]);
}
}
console.log(
'Registrations with payment ID and username added',
registrations
);
saveRegistrations(registrations, callback);
};
// generates the registrations array from the cart items
var generateRegistrationsArray = async function(order) {
console.log('Generating array from cart items');
var arr = [];
for (var i = 0; i < order.cart.items.length; i++) {
var players = await generatePlayersArray(order.cart.items[i]);
console.log('Players: ', players);
// create registration, using new players array
arr.push({
event: order.cart.items[i].event,
field: order.cart.items[i].field,
day: order.cart.items[i].day,
division: order.cart.items[i].division,
level: order.cart.items[i].level,
group: order.cart.items[i].group,
numplayers: order.cart.items[i].numberofplayers,
price: order.cart.items[i].price,
players: players,
createdOn: new Date(),
net: null,
team: null,
notes: null,
paymentNote: null,
waiversSent: false,
reviewed: false,
active: true
});
console.log('Registrations:', arr);
}
console.log('Registrations array', JSON.stringify(arr));
return arr;
};
// generates the player array from the cart and AVP info
var generatePlayersArray = async function(items) {
console.log('Generating players array from cart items');
var parr = [];
for (var i = 0; i < items.players.length; i++) {
var pinfo = {
last: items.players[i].last,
avp_id: items.players[i].avp_id
};
var avpinfo = await getAVPPlayerInfo(pinfo);
console.log('AVP info returned', avpinfo);
// create registration, using new players array
parr.push({
first: avpinfo.first || items.players[i].first,
last: avpinfo.last || items.players[i].last,
email: items.players[i].email,
address: avpinfo.address || items.players[i].address,
city: avpinfo.city || items.players[i].city,
state: avpinfo.state || items.players[i].state,
zip: avpinfo.zip || items.players[i].zip,
country: items.players[i].country,
phone: avpinfo.phone || items.players[i].phone,
signed: false,
sandbagger: false,
waivers: [],
createdOn: new Date(),
adult: avpinfo.adult || items.players[i].adult,
avp_id: items.players[i].avp_id,
ranking: items.players[i].ranking,
overallRanking: items.players[i].overallRanking,
shirt_size: items.players[i].shirtSize
});
console.log(parr);
}
console.log('Players array', JSON.stringify(parr));
return parr;
};
// get AVP data to update player info
var getAVPPlayerInfo = function(pinfo) {
console.log('Admin AVP API begin', pinfo);
avp_id = pinfo.avp_id;
last = pinfo.last;
// GET THE AVP DATA
var options = {
uri: `http://api.volleyamerica.com/VAREST.svc/V6rHHdPO/players/${avp_id}`,
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
rp(options)
.then(function(resp) {
console.log('Player has data', resp);
let avp = {};
let address = resp.Address1 || null;
if (resp.Address2) {
address += ` ${resp.Address2}`;
}
// GET AVP member data (shirt size and ranking for db)
avp.avp_id = avp_id;
avp.shirtSize = resp.AdultShirtSize;
avp.first = resp.FirstName;
avp.last = resp.LastName;
avp.email = resp.Email || null;
avp.phone = resp.HomePhone || null;
avp.address = address || null;
avp.signed = false;
avp.sandbagger = false;
avp.waivers = [];
avp.city = resp.City || null;
avp.state = resp.State || null;
avp.zip = resp.Zip || null;
// get overallRankings greater than 0
avp.overallRanking = _.pickBy(
_.get(resp, 'OverallRanking', {}),
(ranking, key) => {
return ranking > 0;
}
);
// get rankings greater than 0
avp.ranking = _.pickBy(_.get(resp, 'Ranking', {}), (ranking, key) => {
return ranking > 0;
});
let adult = null;
// if player is under 18
const birthday = moment(resp.Birthdate, 'M/D/YYYY');
age = moment().diff(birthday, 'years', false);
if (age < 18) {
avp.adult = false;
} else {
avp.adult = true;
}
console.log('AVP API SUCCESS, returning avp data ', avp);
return avp;
})
.catch(function(err) {
console.log('AVP data error', err);
});
};
//save registrations to the database
var saveRegistrations = function(registrations, callback) {
console.log('SAVING REGISTRATIONS TO DB', registrations);
// var reginfo = registrations;
Registration.collection
.insert(registrations)
.then(function(r) {
console.log('Successfully saved registrations!', r.insertedCount);
nodemailerConfirmation(registrations, callback);
})
.catch(function(err) {
console.log(err);
for (var i in err.writeErrors) {
console.log(err.writeErrors[i].errmsg);
}
callback(err.message);
});
};
检查上面的代码。