我需要执行2 firebase calls
来从数据库中检索特定数据。 Once these promises resolve
,我想用检索到的数据调用另一个函数。我怎样才能做到这一点? .. Promise.All?
以下代码:
app.post('/testtwilio', function(req, res) {
//save request variables
var to_UID = req.body.to;
var from_UID = req.body.from;
var experience_id = req.body.exp_id;
//Query firebase and save 'zone_id' which we need later
firebase.database().ref('experiences').child(experience_id).once('value').then((snap) => {
zone_id = snap.val().ZoneID;
});
//Query firebase and save 'from_name' which we need later
firebase.database().ref('users').child(from_UID).once('value').then((snap) => {
from_name = snap.val().Name;
});
//Once we have the two variables returned and saved
//Call a final firebase query and a twilio function with all the recieved data
firebase.database().ref('users').child(to_UID).once('value').then((snap) => {
//Do something with this aggregated data now
client.messages.create({
//blah blah do something with the saved data that we retrieved
var phone = snap.val().Phone;
var msg = from_name + zone_id + from_UID + experience_id
});
});
});
答案 0 :(得分:3)
是的,您可以使用$EmailFrom = $_REQUEST['email'];
$SKU = $_REQUEST['sku'];
$SIZE = $_REQUEST['size'];
$ID = $_REQUEST['pridpr'];
$EmailTo = "info@example.com";
$Subject = "Contact for Size";
$DATENOW = date('d/m/Y h:i:s a', time());
$Body = "";
$Body .= "<b>Email:</b> ";
$Body .= $EmailFrom . "\r\n";
$Body .= "<br>";
$Body .= "<b>Product SKU:</b> ";
$Body .= $SKU . "\n";
$Body .= "<br>";
$Body .= "<b>Product ID:</b> ";
$Body .= $ID . "\n";
$Body .= "<br>";
$Body .= "<b>Product Size:</b> ";
$Body .= $SIZE;
$Body .= "<br>";
$Body .= "<b>Date:</b> ";
$Body .= $DATENOW;
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\n";
$headers .= "From: ". $EmailFrom ."" . "\n";
$success = mail($EmailTo, $Subject, $Body, $headers);
if ($success){
echo '<div class="alert alert-success">Done!</div>';
$file = fopen('request-size.csv', 'a');
$data = array(
array($EmailFrom, $SKU, $ID, $SIZE, $DATENOW)
);
foreach ($data as $row)
{
fputcsv($file, $row);
}
fclose($file);
}
else{
echo '<div class="alert alert-danger">Error!</div>';
}
,因为Promise.all
会返回一个。
快速肮脏的例子:
once('value')
答案 1 :(得分:1)
如果您使用的是7.6及更高版本的NodeJS,您也可以使用异步函数编写此代码,这样可以更加简单地阅读和维护
// ...
const wrap = require('express-async-wrap')
// ...
// need to wrap async function
// to make it compatible with express
app.post('/testtwilio', wrap(async (req, res) => {
const to_UID = req.body.to
const from_UID = req.body.from
const experience_id = req.body.exp_id
const [
snap1,
snap2,
snap3
// waiting for all 3 promises
] = await Promise.all([
firebase.database().ref('experiences').child(experience_id).once('value'),
firebase.database().ref('users').child(from_UID).once('value'),
firebase.database().ref('users').child(to_UID).once('value')
])
const zone_id = snap1.val().ZoneID
const from_name = snap2.val().Name
const phone = snap3.val().Phone
const msg = from_name + zone_id + from_UID + experience_id
// ...
client.messages.create(...)
}))