【问题标题】:I can't get the return statement to work properly我无法让 return 语句正常工作
【发布时间】:2018-10-26 09:23:38
【问题描述】:

我正在尝试返回一个值并将其显示在控制台中。(现在) 用户 ID 被传递给“函数”。 该函数将在数据库中检查此 ID,并应返回用户名。 我知道找到了正确的数据,因为 console.log 在函数本身中返回了正确的信息(请参阅:THIS WORKS) 但是使用return childData.userName;时返回值是“未定义”

调用函数

console.log( f_returnUserDetails(uid)

函数本身

  function f_returnUserDetails(a){
    console.log(a)
    var key;
    var childData;
    firebase.database().ref('/dataman-blabla/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
      snapshot.forEach(function(childSnapshot) {
          key = childSnapshot.key;
          childData = childSnapshot.val();
          console.log(childData.userName); //THIS WORKS
          return childData.userName; //THIS DOES NOT
      });
    });
  };

【问题讨论】:

  • 您还需要在 firebase 对象之前添加 return 并检查
  • firebase 对象之前是什么意思,那在哪里?

标签: javascript firebase


【解决方案1】:

你必须返回 promise,因为这个函数是异步的。

    function f_returnUserDetails(a){
      console.log(a)
      var key;
      var childData;
      return new Promise(function(resolve, reject) { //return promise
        firebase.database().ref('/dataman-blabla/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
          snapshot.forEach(function(childSnapshot) {
            key = childSnapshot.key;
            childData = childSnapshot.val();
            console.log(childData.userName);
            resolve(childData.userName); 
          });
        });
      });
    };
   // function call should be like this   
   f_returnUserDetails(uid).then((username) => {
     console.log(username);
   });

【讨论】:

  • 这会在下面的Promise {<pending>} __proto__: Promise catch: ƒ catch() constructor: ƒ Promise() finally: ƒ finally() then: ƒ then() Symbol(Symbol.toStringTag): "Promise" __proto__: Object [[PromiseStatus]]: "pending" [[PromiseValue]]: undefined中返回
  • 这是一个承诺。它应该看起来像这样。异步函数不能合理地返回任何其他内容(除非它允许回调)。
  • 确保在函数调用后添加.then
  • 你会在.then回调中得到你的回复
  • 请原谅我的无知,这对我来说是新的。如何从 Promise 中提取价值?
【解决方案2】:

我不是 100% 确定,但我认为 .forEach 在休息和返回方面表现不佳。尝试将其更改为传统的 for of 循环,看看是否可行。

来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description

没有其他方法可以停止或中断 forEach() 循环 抛出异常。如果你需要这样的行为,forEach() 方法 是错误的工具。

return firebase.database().ref('/dataman-blabla/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
      for(childSnapshot of snapshot) {
          key = childSnapshot.key;
          childData = childSnapshot.val();
          console.log(childData.userName); //THIS WORKS
          return childData.userName; //THIS DOES NOT
      });
    });

【讨论】:

  • console.log 返回正确的值(只有正确的值)
  • 返回是什么意思?它只是在控制台中记录值。它不会把它传回去。我还认为您需要在 firebase 调用中添加返回。请参阅我的更新答案。
  • 回调和承诺是一种非此即彼的情况。 on 每次都会触发回调;不能使用回调的返回值。 once 将返回一个承诺,该承诺将在该事件的第一个实例中实现,并将该值传递给 then 处理程序,然后您可以通过从 then 处理程序返回一个值来链接到一个新的承诺。
  • @B.Blunt 您的代码返回以下内容:ƒ (snapshot) { for(childSnapshot of snapshot) { key = childSnapshot.key; childData = childSnapshot.val(); console.log(childData.userName); //THIS WORKS
【解决方案3】:

调用新函数并传递响应

function f_returnUserDetails(a){
    console.log(a)
    var key;
    var childData;
    firebase.database().ref('/dataman-blabla/').orderByChild("uid").equalTo(a).on('value', function (snapshot) {
      snapshot.forEach(function(childSnapshot) {
          key = childSnapshot.key;
          childData = childSnapshot.val();
          console.log(childData.userName); //THIS WORKS
          processData(childData.userName); 
      });
    });
  };


function processDate(name){
   // You have your name here.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 2013-11-23
    • 2020-09-20
    • 2016-04-05
    • 1970-01-01
    • 2016-03-05
    相关资源
    最近更新 更多