简体   繁体   English

Cloud firestore和nextjs - getInitialProps和async数据

[英]Cloud firestore and nextjs - getInitialProps and async data

Im attempting to retrieve a document using the getInitialProps method from nextjs. 我试图使用nextjs中的getInitialProps方法检索文档。 However it is returning a Promise with a state 'pending'. 然而,它正在返回一个状态为“待定”的Promise。 Here is my code: 这是我的代码:

  static async getInitialProps(context) {
    const { id } = context.query;

    await db
      .collection('clients')
      .doc('JJqyDI8a1ILqnqmp2gcO')
      .get()
      .then(doc => ({
        data: doc.data(),
      }));

    console.log(data); // logs pending

    return {
      client: data,
    };
  }

I cant seem to find any examples either. 我似乎也找不到任何例子。

All async functions return a promise. 所有异步函数都返回一个promise。

I don't see how you're accessing data because when you console.log it is already out of scope. 我不知道你是如何访问data因为当你在console.log时它已经超出了范围。

Do something like this: 做这样的事情:

  static async getInitialProps(context) {
    const { id } = context.query;

    const data = await db
      .collection('clients')
      .doc('JJqyDI8a1ILqnqmp2gcO')
      .get()
      .then(doc => ({
        ...doc.data(),
      }));


    return {
      client: data,
    };
  }

This should make it accessible by props.client 这应该使props.client可以访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM