简体   繁体   English

答应pnp / sp的麻烦

[英]Promise troubles with pnp/sp

I am building an application customiser in SPFX and I am using pnp/sp to get data from a Sharepoint list - all easy so far. 我正在SPFX中构建应用程序定制程序,并且正在使用pnp / sp从Sharepoint列表中获取数据-到目前为止,一切都很容易。 I have figured out the code like this, but it is just returning [object promise] here is my code , any help would be brilliant. 我已经弄清楚了这样的代码,但是它只是返回[object promise]这是我的代码,任何帮助都将是很棒的。

I am calling the function like this : 我正在这样调用函数:

public emailAddressGetter = this.GetSharePointData();

I am trying to show the output like this : 我试图显示这样的输出:

${escape(this.emailAddressGetter.toString())}

and this is the promise I am executing : 这就是我正在执行的承诺:

  private async GetSharePointData(): Promise<any>
{ 
    let myVar : string;
    var resultData: any = await sp.web.lists
                               .getByTitle('Emails')
                               .items                                     
                               .select('EmailAddress')
                               .getById(99)
                               .get().then((r => {
   myVar = r.EmailAddress;
                         }));                            
    console.log(myVar);
    return myVar;

}

any help would be appreciated, I know I am almost there :) thanks guys 任何帮助将不胜感激,我知道我快到了:)谢谢大家

I think your GetSharePointData returns a Promise, because it has async declaration , so you need to execute code asynchronously and wait for the result. 我认为您的GetSharePointData返回一个Promise,因为它具有async declaration ,因此您需要异步执行代码并等待结果。 Instead of: 代替:

public emailAddressGetter = this.GetSharePointData();

${escape(this.emailAddressGetter.toString())}

Try: 尝试:

this.GetSharePointData()
  .then(res => {
    // res here is myVar
    ${escape(res.toString())};
  });

Firstly fix your code's type annotations. 首先修复代码的类型注释。 You are completely defeating the point of TypeScript by suppressing the errors the language exists to catch by specifying vacuous types instead of leveraging inference. 通过指定空虚类型而不是利用推理来抑制语言存在的错误,从而完全克服了TypeScript的观点。 This isn't Java. 这不是Java。

async GetSharePointData() { // return type is inferred as `Promise<string>`
    const result = await sp.web.lists // the `any` you had here was worse than useless. 
                           .getByTitle('Emails')
                           .items                                     
                           .select('EmailAddress')
                           .getById(99)
                           .get();
     const emailAddress= result.emailAddress;
     console.log(emailAddress);
     return emailAddress;
}

Now onto async functions and promises. 现在进入异步功能和承诺。 An async function or method always returns a promise. async函数或方法总是返回promise。 Assigning the result of calling such a function directly to a property or variable will always result in the behavior you described 将直接调用此类函数的结果分配给属性或变量将始终导致您描述的行为

GetSharePointData().toString() === "[object Promise]"

The correct approach to setting the property emailAddressGetter (BTW that's a terrible name for that property either way) to the email address that the promise eventually resolves with depends on the context, but here is something you might do. 将属性emailAddressGetter (顺便说一句,该属性都是该属性的可怕名称)设置为承诺最终解析所使用的电子邮件地址的正确方法取决于上下文,但是您可以执行此操作。

constructor() {
    this.emailAddressPromise = this.GetSharePointData();
    this.emailAddressPromise.then(emailAddress => this.emailAddress = emailAddress);
 }

But that could be awful and unnecessary unpredictable depending on what you are trying to do. 但这可能是可怕的和不必要的不​​可预测的,具体取决于您要执行的操作。

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

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