全局变量未携带过量Javascript FCF

时间:2018-08-11 01:53:14

标签: javascript firebase google-cloud-functions

我正在使用Firebase云函数,在其中定义变量commentIdSpecific。当我将其记录在函数中时:-console.log(comm id ${commentIdSpecific});-打印其值。当我尝试在此处打印时:-console.log(test of variables inisde of post: ${usernameWhoOwnsThePost}, uwotpi: ${commentIdSpecific})-返回未定义。我看过三个谈论全局变量的网站,它与我在这里的内容似乎没有什么不同。

如何使第二个打印语句中的值等于第一个打印语句中的值?预先感谢。

var commentIdSpecific;
  db.ref(`/users/${usernameWhoOwnsThePost}/posts/${usernameWhoOwnsThePostID}/comments`).once('value').then(snap => {
      commentIdSpecific = snap.val();
      let ids = [];
         for (var id in snap.val()) {
           ids.push(id);
         }
  let lastValueId = ids[ids.length - 1]
  console.log(`last id value ${lastValueId}. UserPost: ${usernameWhoOwnsThePost}. user owner post id: ${usernameWhoOwnsThePostID}...`);
  commentIdSpecific = lastValueId;
  console.log(`comm id ${commentIdSpecific}`);
      return commentIdSpecific;
    }).catch(err => {
    console.log(err);
  });


  var commentPoster;
  db.ref(`/users/${usernameWhoOwnsThePost}/posts/${usernameWhoOwnsThePostID}/comments/${commentIdSpecific}/comment`).once('value').then(snap => {
      commentPoster = snap.val();
      console.log(`commentPoster: ${snap.val()}`);
      console.log(`test of variables inisde of post: ${usernameWhoOwnsThePost}, uwotpi: ${commentIdSpecific}`)
      return commentPoster
    }).catch(err => {
    console.log(err);
  });

2 个答案:

答案 0 :(得分:1)

once()是异步的,并立即以承诺指示返回,该承诺指示异步工作何时完成。同样,then()立即返回承诺。每当查询结果完成时,传递给then()的回调都会在未知的时间内执行。在此之前,您的代码将继续在下一行执行,这意味着commentIdSpecific在首次访问时将是未定义的。

您需要使用一个Promise链来确保依赖于异步工作结果的工作只有在其可用后才能被访问。

您可能希望在此页面上观看有关JavaScript承诺的视频,以便更好地了解如何在Cloud Functions中使用它们。了解它们如何编写有效的代码绝对至关重要。

https://firebase.google.com/docs/functions/video-series/

答案 1 :(得分:0)

一旦第一个诺言这样解决,您应在诺言链中进行第二个db.ref调用:

  db.ref(`/users/${usernameWhoOwnsThePost}/posts/${usernameWhoOwnsThePostID}/comments`).once('value')
.then(snap => {
      commentIdSpecific = snap.val();
      let ids = [];
         for (var id in snap.val()) {
           ids.push(id);
         }
  let lastValueId = ids[ids.length - 1]
  console.log(`last id value ${lastValueId}. UserPost: ${usernameWhoOwnsThePost}. user owner post id: ${usernameWhoOwnsThePostID}...`);
  commentIdSpecific = lastValueId;
  console.log(`comm id ${commentIdSpecific}`);
      return commentIdSpecific;
    })
.then(commentIdSpecific => {
db.ref(`/users/${usernameWhoOwnsThePost}/posts/${usernameWhoOwnsThePostID}/comments/${commentIdSpecific}/comment`).once('value').then(snap => {
      commentPoster = snap.val();
      console.log(`commentPoster: ${snap.val()}`);
      console.log(`test of variables inisde of post: ${usernameWhoOwnsThePost}, uwotpi: ${commentIdSpecific}`)
      return commentPoster
    }).catch(err => {
    console.log(err);
  });

})
.catch(err => {
    console.log(err);
  });

once()是一个异步操作,因此console.log(test of variables inside of post: ${usernameWhoOwnsThePost}, uwotpi: ${commentIdSpecific})可能在commentIdSpecific = snap.val();commentIdSpecific = lastValueId;

之前执行

因此,您需要做的是首先完成db.ref(/users/${usernameWhoOwnsThePost}/posts/${usernameWhoOwnsThePostID}/comments),然后在链中下一个db.ref(/users/${usernameWhoOwnsThePost}/posts/${usernameWhoOwnsThePostID}/comments/${commentIdSpecific}/comment)中调用.then()

当您从第一个commentIdSpecific返回.then()时,第二个.then()会将其作为参数。

https://javascript.info/promise-chaining将帮助您更深入地挖掘.then chaining