如何在云函数中解析/返回forEach函数

时间:2017-11-24 09:58:02

标签: javascript firebase google-cloud-functions google-cloud-firestore

我有以下(示例)数组:

pages = [ {
  "name" : "Hello",
  "id" : 123
},{
  "name" : "There",
  "id" : 987
},{
  "name" : "Great",
  "id" : 555
}  ];

我想将此数组中的每个对象保存为Collection中的Document。 因此,我有以下功能:

exports.testSaveFacebookPages = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    let userUid = req.body.uid  

// pages array is available here...

    const PagesRef = admin.firestore().collection(`/users/${userUid}/pages/`)
    return pages.forEach(function(page, index){
      PagesRef.doc(`p${index}`).update(page, { merge: true })
    });

    res.status(200).send('Pages saved successfull!');
  }); // cors...
}); // exports...

当函数执行时,它将页面保存到Firestore :) 但似乎函数在循环中执行。记录说:

Function execution started
Function execution took 60002 ms, finished with status: 'timeout'

我已经开头了:Cloud Functions with Firestore error "Dealine Exceeded"https://firebase.google.com/docs/functions/terminate-functions

但我找不到任何带有forEach的Cloud Function示例。 任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

在您发送回复之前,您已退出功能。如果您从未发送回复,您的HTTPS功能将会超时。

相反,您应该将所有承诺从所有收集到数组中,然后在发送最终响应之前等待所有这些承诺解析。像这样:

<table v-for="piste in details.text_tracks">
    <tr>
        <td>{{details.text_tracks}}</td>
    </tr>
</table>

var details.text_tracks = "Jenny Lewis . Pretty Bird Sergio Mendes & 
Brasil 66 .\n For What it’s Worth Petra Magoni feat. Jean-Jacques 
Milteau & Manu Galvin .\n Fever Emiliana Torrini . Me and Amini Cowboy 
Junkies .\n Run for your Life Mira Billotte . As I Went out in the 
Morning Nitin Sawhney feat.\n Tina Grace . October Daze Isobel Campbell 
.\n Saturday’s Gone Barbara . Mes Hommes...\n"