Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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 将异步POST发送到数据库时出现问题_Javascript_Angular_Asynchronous_Post - Fatal编程技术网

Javascript 将异步POST发送到数据库时出现问题

Javascript 将异步POST发送到数据库时出现问题,javascript,angular,asynchronous,post,Javascript,Angular,Asynchronous,Post,目前我有以下代码,基本上是将表单的结果发布到数据库中,但有时候表单中的某些字段可能为null,因此我必须在更新之前检查对象并保存变量,以使它们不会变为null onUpdateClick(updateHash, updateHeight, updateSize, updateTime) { //this is the url where the post will be made this.url = "http://localhost:3000/api/blockinfo/"

目前我有以下代码,基本上是将表单的结果发布到数据库中,但有时候表单中的某些字段可能为null,因此我必须在更新之前检查对象并保存变量,以使它们不会变为null

onUpdateClick(updateHash, updateHeight, updateSize, updateTime) {
    //this is the url where the post will be made
    this.url = "http://localhost:3000/api/blockinfo/" + updateHash.toString();

    //those are the variables in the object stored in the database (url)
    var height;
    var size;
    var time;

    //this is the original object before any modification
    var nullCase;

    nullCase = this.http.get(this.url)

    //those if's mean that if one of the fields in the form is null (non filled) , I will check for the object before the modification (nullCase),and use its previous values in the update
    if (updateHeight == null) {
      height = Number(nullCase.height)
    } else {
      height = Number(updateHeight)
    }

    if (updateSize == null) {
      size = Number(nullCase.size)
    } else {
      size = Number(updateSize)
    }

    if (updateTime == null) {
      time = Number(nullCase.time)
    } else {
      time = Number(updateTime)
    }



    //after all the above checks, I want my current object to get its values assigned and ready to be posted
    this.body = {
      "hash": updateHash.toString(),
      "height": height,
      "size": size,
      "time": time
    }

    //after the object to post is ready, I want to make the post into the database
    this.http.post(this.url, this.body).subscribe((result) => {
      console.log(result)
    });


  }
但是似乎所有的东西都不同步了,因为除了检查您正在为nullCase分配订阅之外,我还得到了null对象,这是没有意义的

nullCase = this.http.get(this.url).subscribe(result => {}); // Wrong
this.http.get(this.url).subscribe(result => { nullCase = result });
2) Get是异步的,您需要让代码依赖于回调函数中的结果

// Wrong
this.http.get('url').subscribe(result => { nullCase = result });
this.http.post('url', {}).subscribe(result => { // Something using nullCase });

// Should be
this.http.get('url').subscribe(result => {
  this.http.post('url', {});
});
3) 更好的是,您不应该嵌套订阅,您应该使用
rxjs
操作符:

this.http.get("").pipe(
  switchMap(result => {
    return this.http.post("", {});
  })
).subscribe();

很可能,
this.http.post
是在调用
this.http.get
的回调之前调用的。您需要移动
get
回调末尾的
post