无法存储POST方法的响应

时间:2019-04-17 11:32:09

标签: angular httpclient

我能够console.log POST方法的响应。 但是我想将此响应存储在变量中,以在代码中进一步使用它。 请帮忙。

post方法调用发送一个对象作为响应。

student.ts(“学生”是一个类,与从发帖请求中接收的对象相同。)

export class Student {
ID : number;
firstName : string;
}

component.ts(在提交表单时调用submit方法。this.student提供未定义的输出)

public student : Student[];

onSubmit() {
    this._studentService.addData(this.addStudentForm.value)
      .subscribe( response => console.log("respnse", response),
        );
    console.log(this.student);
  }

service.ts

addData(studentData) : Observable<any> {
    return this.http.post<any>(this._url, studentData);
  }

当我尝试将响应存储到变量中时,我得到的输出未定义。 如何将响应存储在变量中?

2 个答案:

答案 0 :(得分:0)

您应该只在订阅中添加this.student = response;

this._studentService.addData(this.addStudentForm.value).subscribe( response => {
    console.log("respnse", response);
    this.student = response;
});

但是,如果您想对student进行操作,则应在next的订阅回调中执行此操作:

this._studentService.addData(this.addStudentForm.value).subscribe( response => {
    console.log("respnse", response);
    this.student = response;
    console.log(this.student) <- THIS WILL WORK - STUDENT IS NOT UNDEFINED
    // HERE ARE YOUR METHODS WHICH ARE USING STUDENT
});

在订阅之后,此console.log仍将是未定义的,因为此代码将在订阅结束前执行,因此student暂时为undefinded

答案 1 :(得分:0)

尝试:

other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "ner"]
with nlp.disable_pipes(*other_pipes):
    optimizer = nlp.begin_training()
    for i in range(10):
         random.shuffle(train_data)
         for text, annotations in train_data:
             nlp.update([text], [annotations], sgd=optimizer)```

Error Traceback:
```Traceback (most recent call last):

  File "<ipython-input-1-b5f869eaaf43>", line 1, in <module>
    runfile('/home/abhishek/Desktop/Monster/Resume_Parser/MI_Resume/skills_ner.py', wdir='/home/abhishek/Desktop/Monster/Resume_Parser/MI_Resume')

  File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/abhishek/Desktop/Monster/Resume_Parser/MI_Resume/skills_ner.py", line 234, in <module>
    nlp.update([text], [annotations], sgd=optimizer)

  File "/home/abhishek/.local/lib/python3.6/site-packages/spacy/language.py", line 452, in update
    proc.update(docs, golds, sgd=get_grads, losses=losses, **kwargs)

  File "nn_parser.pyx", line 413, in spacy.syntax.nn_parser.Parser.update

  File "nn_parser.pyx", line 516, in spacy.syntax.nn_parser.Parser._init_gold_batch

  File "ner.pyx", line 106, in spacy.syntax.ner.BiluoPushDown.preprocess_gold

  File "ner.pyx", line 165, in spacy.syntax.ner.BiluoPushDown.lookup_transition

KeyError: "[E022] Could not find a transition with the name 'U-SKILL' in the NER model."```