javascript - 是什么原因导致 "Error: Uncaught (in promise): Response with status:200 for Url:null"出现?

标签 javascript node.js angular nativescript

我正在通过 NodeJS 和 Express 访问 Mongo 数据库,如下所示:

var MongoClient = require('mongodb').MongoClient;
    ...
    app.get("/app/visits", function (req, res, next) {
      console.log("get visits");
      MongoClient.connect('mongodb://localhost:27017/db', function (err, db) {
        if (!err) { console.log("We are connected"); }
        visits = db.collection('visits', function (err, collection) { });
        visits.find().toArray(function (err, user) {
          this.user = JSON.stringify(user);
          if (err) { throw err; } else console.dir(this.user);
        });
        res.send(this.user);
      });
    });

在浏览器中这工作正常。如果我将 res.send(this.user); 更改为 res.status(301).send(this.user); 状态也会更改。 但问题是,使用 native 脚本代码的 Angular 2 返回错误:

getActualVisits()
     {  
        return this.http.get("http://localhost:1234/app/visits").map(response => response.json())
      }

在尝试修复 7 小时后,我不知道为什么。 方法 getActualVisits() 调用自:

 getActualSpecialization() {
    let v = this.getActualVisits();
...
}

最佳答案

您需要在 .map 之后调用 .subscribe 以观察返回的值。

getActualVisits() {  
    return this.http.get("http://localhost:1234/app/visits")
        .map(response => response.json())
        .subscribe(
            data => this.actualVisits = data,
            err => this.logError(err),
            () => console.log('get actual visits complete')
         );
}

有关更多信息,请参阅以下文档 https://auth0.com/blog/2015/10/15/angular-2-series-part-3-using-http/

关于javascript - 是什么原因导致 "Error: Uncaught (in promise): Response with status:200 for Url:null"出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528606/

相关文章:

javascript - 从 URL 返回 innerHTML

node.js - 如何在客户端正确显示服务器请求的数据

angular - 如何使用 Angular 发送带有对象参数的 fetch api 请求?

javascript - 在 page.evaluate 中等待 page.waitFor ?

javascript - 使用 jQuery 更改模式中的 div 类

javascript - $.extend 排除一些 JSON 对象

javascript - 使用倒数计时器自动刷新页面

javascript - 为什么我的 mysql 日期在 javascript 中减少一天?

angular - 我如何订阅可观察对象的 Angular 模型的变化?

angular - 如何在 Angular 2 应用程序中显示 SSRS 报告