Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从导入模块中的函数返回变量_Javascript_Node.js_Webpack - Fatal编程技术网

Javascript 从导入模块中的函数返回变量

Javascript 从导入模块中的函数返回变量,javascript,node.js,webpack,Javascript,Node.js,Webpack,我正在用webpack设置一个Framework7项目,我想要一个使用Firebase的数据库请求模块。导入模块中的函数运行,但请求的数组从未返回/承诺未解析。我做错了什么 我怀疑我在导入模块本身时做了一些错误的事情,或者我没有正确地解析承诺。我在模块的getAllRestaurants函数中尝试了返回和解析,但都失败了。我尝试使用async/await、.then()甚至setTimout等待来自数据库调用的响应。我还试着说let array=database.getAllRestaurant

我正在用webpack设置一个Framework7项目,我想要一个使用Firebase的数据库请求模块。导入模块中的函数运行,但请求的数组从未返回/承诺未解析。我做错了什么

我怀疑我在导入模块本身时做了一些错误的事情,或者我没有正确地解析承诺。我在模块的getAllRestaurants函数中尝试了返回和解析,但都失败了。我尝试使用async/await、.then()甚至setTimout等待来自数据库调用的响应。我还试着说let array=database.getAllRestaurants()

database.js(模块)

app.js(main)


我希望app.js中的代码从函数中获取数组,但它只获取“undefined”

getAllRestaurants中没有返回语句。试一试

export function getAllRestaurants(){
    //defining db route omitted
    let array = [];
    return route.get().then(function(querySnapshot){
并跳过这一行:

    .resolve(array)  //Have tried with only return and with only resolve

我会将您的代码更改为:

export function getAllRestaurants(){
    // Return a new Promise, which will either resolve a value or reject an error.
    return new Promise(function(resolve, reject){
        //defining db route omitted
      let array = [];
      // Nothing changed here.
      route.get().then(function(querySnapshot){
          querySnapshot.docs.forEach(function(document){
              array.push(document.data());
          });
          resolve(array);
          // Replaced the below line with the one above.
          //return array; //Have tried with only return and with only resolve
      })
      // No need for the line below.
      // resolve(array)  //Have tried with only return and with only resolve
      .catch(function(error){
          console.error('error i firebase getallrestaurants: ', error);
          // Added a line here to reject the error.
          reject(error);
      });
    });
}
我已经评论了上面的编辑。通过这种方式,您将始终从该方法中获得承诺,您可以这样使用它:

//importing my module
import * as database from './database';

database.getAllRestaurants().then(array =>{
  //do stuff
}).catch(err => {
   console.log('an error happened!');
});

由于
getAllRestaurants()
基于异步操作(数据库操作),因此不能简单地从中返回值。但是,您可以返回一个承诺,并让客户机代码预期到这一点。您的示例中没有一个能够工作,因为第一种情况(让array one)会产生类似于同步操作的结果。然而,它是异步的,实际上什么也不返回。第二种情况要求返回一个承诺,但没有返回任何承诺。您应该返回一个承诺,并异步处理结果。我希望
返回route.get()。然后(…其余的代码…
应该可以工作。否则路由有问题,特别是
.resolve()
对结果做了什么。您有关于
.resolve()的文档吗
会回来吗?这是框架7的一部分吗?成功了。谢谢!我似乎有点误解了承诺。
export function getAllRestaurants(){
    // Return a new Promise, which will either resolve a value or reject an error.
    return new Promise(function(resolve, reject){
        //defining db route omitted
      let array = [];
      // Nothing changed here.
      route.get().then(function(querySnapshot){
          querySnapshot.docs.forEach(function(document){
              array.push(document.data());
          });
          resolve(array);
          // Replaced the below line with the one above.
          //return array; //Have tried with only return and with only resolve
      })
      // No need for the line below.
      // resolve(array)  //Have tried with only return and with only resolve
      .catch(function(error){
          console.error('error i firebase getallrestaurants: ', error);
          // Added a line here to reject the error.
          reject(error);
      });
    });
}
//importing my module
import * as database from './database';

database.getAllRestaurants().then(array =>{
  //do stuff
}).catch(err => {
   console.log('an error happened!');
});