Node.js 节点/Express API上连接之间的数据持久化

Node.js 节点/Express API上连接之间的数据持久化,node.js,express,Node.js,Express,我正在为MongoDB构建一个Node/Express API前端,它可以很好地用于单个连接,但不能用于2个或更多连接 如果我同时使用两个连接调用API,那么被调用函数中的数据就会损坏。代码摘录: // This is the Express entry point app.get('/user', async (request, response) => { const readResp = await read(request) response.send(readRe

我正在为MongoDB构建一个Node/Express API前端,它可以很好地用于单个连接,但不能用于2个或更多连接

如果我同时使用两个连接调用API,那么被调用函数中的数据就会损坏。代码摘录:

// This is the Express entry point
app.get('/user', async (request, response) => {
    const readResp = await read(request)
    response.send(readResp)
})


// The called function
async function read(request) {
    //
    try {

        // This gets data from MongoDB via Mongoose
        var connections = await Connection.find() 

        // Do other stuff here with connections,
        // but part-way through the connections 
        // variable gets corrupted as the new request has come in

        return { error: { code: 0, message: 'OK' }, connections: connections }
    } catch (error) {
        Log.errorToConsoleAndClose(error)
    }
}
我使用express等待传入请求,启动
read()
函数并返回响应

当第二个请求同时出现时,问题就出现了,因为它似乎没有使用
read()
的新实例,相反,当var
连接被新的传入请求重置时,我的代码在某个点失败


任何线索/帮助都将不胜感激。

我遇到了一些与使用异步的其他数据库类似的东西

解决方案是在异步请求之外创建连接,以便保持其打开状态,然后通过发送多个查询

每次调用异步时,都会发生重置连接而不完成请求的情况

其中一个解决方案是使用。然后承诺

下面是一个使用承诺的例子

  router.post('/api/get_data', (req, res, next) => {
  try {
  MongoClient.connect(connectionStr, mongoOptions, function(err, client) {
   assert.equal(null, err);
   const db = client.db('db');

   //Step 1: declare promise

   var myPromise = () => {
     return new Promise((resolve, reject) => {

        db
         .collection('your_collection')
         .find({id: 123})
         .limit(1)
         .toArray(function(err, data) {
             err 
                ? reject(err) 
                : resolve(data[0]);
           });
     });
   };

   //Step 2: async promise handler
   var callMyPromise = async () => {

      var result = await (myPromise());
      //anything here is executed after result is resolved
      return result;
   };

   //Step 3: make the call
   callMyPromise().then(function(result) {
      client.close();
      res.json(result);
   });
 }); //end mongo client


 } catch (e) {
  next(e)
 }
});
module.exports = router;

有关该主题的更多信息,请访问

我发现了一些与使用async的其他数据库类似的内容

解决方案是在异步请求之外创建连接,以便保持其打开状态,然后通过发送多个查询

每次调用异步时,都会发生重置连接而不完成请求的情况

其中一个解决方案是使用。然后承诺

下面是一个使用承诺的例子

  router.post('/api/get_data', (req, res, next) => {
  try {
  MongoClient.connect(connectionStr, mongoOptions, function(err, client) {
   assert.equal(null, err);
   const db = client.db('db');

   //Step 1: declare promise

   var myPromise = () => {
     return new Promise((resolve, reject) => {

        db
         .collection('your_collection')
         .find({id: 123})
         .limit(1)
         .toArray(function(err, data) {
             err 
                ? reject(err) 
                : resolve(data[0]);
           });
     });
   };

   //Step 2: async promise handler
   var callMyPromise = async () => {

      var result = await (myPromise());
      //anything here is executed after result is resolved
      return result;
   };

   //Step 3: make the call
   callMyPromise().then(function(result) {
      client.close();
      res.json(result);
   });
 }); //end mongo client


 } catch (e) {
  next(e)
 }
});
module.exports = router;

有关该主题的更多信息,请访问

,这里可能有一个直接的或常见的错误,但您的示例太抽象了,很难看出问题所在。您的Manufactedata没有接受任何参数或返回任何内容,这表明您正在对全局数据进行操作。我很想看看这个函数的主体,但是请分享一些可以用来重现你的问题的东西。我已经将代码从sudo改为react。因此,根据你的新描述,你是说腐败发生在你评论过但没有分享的区域的某个地方。这里没有创建全局状态和创建这种腐败的东西,因此这表明问题发生在您没有共享的代码部分。(也是伪的,不是sudo;))这里可能有一个直接的或常见的错误,但是你的例子太抽象了,很难看出问题是什么。您的Manufactedata没有接受任何参数或返回任何内容,这表明您正在对全局数据进行操作。我很想看看这个函数的主体,但是请分享一些可以用来重现你的问题的东西。我已经将代码从sudo改为react。因此,根据你的新描述,你是说腐败发生在你评论过但没有分享的区域的某个地方。这里没有创建全局状态和创建这种腐败的东西,因此这表明问题发生在您没有共享的代码部分。(也是假的,不是sudo;))有道理,谢谢Jono。我来看看我的连接。有道理。谢谢乔诺。我来看看我的连接。