全局变量未结转-Javascript FCF

全局变量未结转-Javascript FCF,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,我正在使用firebase cloud函数,其中定义了一个变量commentIdSpecific。当我在函数中记录它时:-->code>console.log(comm id${commentIdSpecific})--它打印其值。当我尝试在这里打印它时:-->code>console.log(post:${usernamehownsthepost},uwotpi:${commentIdSpecific}中的变量测试)--它返回未定义的。我看过三个关于全球VAR的网站,看起来和我这里的没有什么不

我正在使用firebase cloud函数,其中定义了一个变量commentIdSpecific。当我在函数中记录它时:-->code>console.log(comm id${commentIdSpecific})--它打印其值。当我尝试在这里打印它时:-->code>console.log(post:${usernamehownsthepost},uwotpi:${commentIdSpecific}中的变量测试)--它返回未定义的。我看过三个关于全球VAR的网站,看起来和我这里的没有什么不同

如何将第二个print语句中的值设置为第一个print语句中的值?提前谢谢

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);
  });
once()
是异步的,并立即返回一个承诺,指示异步工作何时完成。类似地,
then()
会立即返回并承诺。传递给
then()
的回调将在查询结果完成后的未知时间内执行。在此之前,您的代码将一直在下一行执行,这意味着首次访问时,
commentIdSpecific
将是未定义的

您需要使用承诺链来确保依赖于异步工作结果的工作只有在可用后才能访问

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


第一个承诺解决后,您应在承诺链中调用第二个
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()
是一个异步操作,因此可能会在
commentIdSpecific=snap.val()之前执行
console.log(post中的变量测试:${usernamehownsthepost},uwotpi:${commentIdSpecific})
commentIdSpecific=lastValueId

因此,您需要做的是首先让
db.ref(/users/${usernamehownshepost}/posts/${usernamehownshepostId}/comments)
完成,然后在下一个
中调用
db.ref(/users/${usernamehownshepost}/posts/${usernamehownshepostId}/comments/${comments specific}/comments)
,然后在链中调用

当您首先返回
commentIdSpecific
表单
.then()
时,它将在第二个
.then()
中作为参数提供


将帮助您挖掘
。然后将
链接到更深层。

感谢@samee的回复,当我复制并粘贴您的代码时,我收到了以下错误:147:9错误,每个然后()应该返回值或抛出承诺/始终返回148:3警告避免嵌套承诺承诺/不嵌套148:3警告避免嵌套承诺承诺/不嵌套您如何避免嵌套承诺?谢谢@DougStevenson,我在js中观看了您的顺序和并行工作,但是你返回了承诺数组(大约在4:40),我不能像这样只返回我的commentIdSpecific:return Promise.all(commentIdSpecific);抱歉,虽然我很喜欢你的视频,但我对js和Firebase功能还是有点陌生。