【问题标题】:Express promise chain error快递承诺链错误
【发布时间】:2017-03-04 05:24:48
【问题描述】:

我编写了一个简单的 express.js 服务器来处理 REST API 请求并从 MongoDB 数据库中获取数据。当我向特定端点(“localhost:8081/api/getUserData”)发出 GET 请求时,promise 链无法按我想要的方式工作,我仍然不明白。

这是我得到的错误: "[TypeError: Cannot read property 'db' of undefined]"

var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var app = express();
var rp = require("request-promise");
var cors = require('cors');

// use it before all route definitions
app.use(cors({ origin: '*' }));

/********************** REST API FUNCTIONS **********************/
app.get('/api/getUserData', function (req, res, next) {
  var context = {};
  console.log("in api getUserData")
  context.db_url = 'mongodb://localhost:27017/test';
  openDatabaseConnection(context)
    .then(getAllUserLocations)
    .then(closeDatabaseConnection)
    .then(function (context) {
      res.send(context.userLocations)
    })
    .catch(function (error) {
      console.log("ERROR :");
      console.log(error);
    })
})
/********************** END REST API FUNCTIONS **********************/

function getAllUserLocations(context) {
  context.db.collection("test").find().toArray().then(function (err, result) {
    console.log("Received from db: " + result.length + " objects");
    context.userLocations = result;
    return context;
  });
}

function openDatabaseConnection(context) {
  console.log("Opening DB connection...");
  return MongoClient.connect(context.db_url)
    .then(function (db) {
      console.log("DB connection opened.");
      context.db = db;
      return context;
    })
}

function closeDatabaseConnection(context) {
  console.log("Closing DB connection");
  return context.db.close()
    .then(function () {
      console.log("DB connection closed");
      return context;
    })
}

/********************** STARTING SERVER **********************/
var server = app.listen(8081, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("Githex server listening at http://%s:%s", host, port)
})

任何帮助都将不胜感激,甚至更多的解释,因为我不明白我做错了什么。

谢谢!

【问题讨论】:

  • 由于context 被定义为只有db_url 属性的对象,当您在getAllUserLocations 函数中使用它时,它没有db 属性
  • 不幸的是,事实并非如此。我从代码中删除了我提供的另一个有效的 api 请求处理程序,但没有指定“db”属性。我只是尝试在承诺链之前添加一个 db 属性,但它也没有解决它..
  • 你是对的,错误表明context在你的一个函数中是未定义的,它不能"read property 'db' of undefined",它可能带有行号和更具体的位置信息。
  • 例如,getAllUserLocations() 并没有真正返回任何东西,它必须是 return context.db.collection(... 假设函数也返回一个承诺。
  • 是的,我添加了“return context.db”等,解决了这个问题,但现在又出现了一个^^,我现在会尝试解决那个问题。非常感谢!

标签: javascript mongodb rest api express


【解决方案1】:

就像第一条评论中提到的@adeneo 一样,您缺少 db 属性。看看你的第一个函数:

app.get('/api/getUserData', function (req, res, next) {
  var context = {};
  console.log("in api getUserData")
  context.db_url = 'mongodb://localhost:27017/test';
  openDatabaseConnection(context)
    .then(getAllUserLocations)
    .then(closeDatabaseConnection)
    .then(function (context) {
      res.send(context.userLocations)
    })
    .catch(function (error) {
      console.log("ERROR :");
      console.log(error);
    })
});

现在遍历这个函数中的行:

  1. 您将上下文设置为空对象
  2. 您在对象上添加了一个 db_url 属性,到目前为止,您已经拥有了

    context = {db_url: "mongodb://localhost:27017/test}
    
  3. 您将上下文对象传递给 openDatabaseConnection 函数
  4. 在 openDatabaseConnection 函数中,您返回一个上下文对象。这个新返回的对象不会在任何地方设置,它只是被返回并且从未使用过。您想使用该返回值调用 getuserlocation() 函数。

所以不要只是调用

.then(getAllUserConnection)

我愿意

.then(function(context){getAllUserConnection(context);})

这应该利用返回的值并确保你正在使用它。

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多