You're looping in a forEach
, which will run for each element. For a simpler example, try this in your Node REPL: [1,2,3].forEach(x => console.log(x) || x)
. Even though it returns, it's only returning and then continuing to the next iteration. You also have one res.send
in the .then
of a a Promise, which could be a further issue if that block gets hit. It looks like what you probably want to do is build up the response object while iterating over betData
, and move your conditions and responses out of the iteration.
Here's an example of how this could work, though without knowing why you have more than one betData
item to loop over I'm not sure if it will be perfect for your use case. Also the async/Promise saveGOTDBet
could continue to be an issue.
let status = 200
let message = ''
betData.forEach(async (data) => {
const storedAddress = `${data.address} ${data.city} ${data.state} ${data.zip}`
const convertedAddress =`${addressData.data[0].delivery_line_1} ${addressData.data[0].last_line}`
if (data.email.toLowerCase() === userInput.email.toLowerCase()) {
status = 400
message = message: `${userInput.email} has already been used this week.`
} else if (storedAddress === convertedAddress) {
status = 400
message = `This address has already been used this week.`
} else {
const saveData = {
firstName: userInput.firstName,
lastName: userInput.lastName,
email: userInput.email,
address: addressData.data[0].delivery_line_1,
city: addressData.data[0].components.city_name,
state: addressData.data[0].components.state_abbreviation,
zip: addressData.data[0].components.zipcode,
favoriteTeam: userInput.favoriteTeam,
terms: userInput.terms,
agreeToEmail: userInput.agreeToEmail
}
await saveGOTDBet(saveData)
status = 200
message = 'Bet successfully placed',
}
})
res.status(status).json({ message })
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…