简体   繁体   English

NodeJs猫鼬如何从“ find()。then”中的“ find()。then”中获取数据?

[英]NodeJs Mongoose How can I get out a data from “find().then” in a “find().then”?

Sorry for my Title, I don't know what can I put. 对不起,我的标题,我不知道可以输入什么。

Can you help me please, I would like to print data from a "then" in a "then" ? 您能帮我吗, 我想在“ then”中打印“ then”中的数据?

Thank you 谢谢

models.book.find()
  .then( function (content) {
    var i = 0;

    while (content[i]) {
      models.author.findOne({"_id": content[i].author_id}, function(err, data) {
        console.log(data); //here, it' good
        content[i] = data;

        MY_DATA = content;

        return MY_DATA;
      });

      i++;
    };
  })
  .then(function (result) {
    console.log(result); // here I would like to print MY_DATA
  });

There are a number of problems with your code, and I don't think it's behaving as you're expecting it to. 您的代码有很多问题,而且我认为它的表现并不符合您的期望。

Chaining Promises 连锁承诺

In order to effectively chain promises how you're expecting, each promise callback needs to return another promise. 为了有效地链接诺言,您期望如何,每个诺言回调都需要返回另一个诺言。 Here's an example with yours changed around a bit. 这是一个示例,您的示例有所更改。

var promise = models.book.find().exec(); // This returns a promise
// Let's hook into the promise returned from 
var promise2 = promise.then( function (books) {
  // Let's only get the author for the first book for simplicity sake
  return models.author.findOne({ "_id": books[0].author_id }).exec(); 
});

promise2.then( function (author) {
  // Do something with the author
});

In your example, you're not returning anything with your callback ( return MY_DATA is returning within the models.author.findOne callback, so nothing happens), so it's not behaving as you're expecting it to. 在您的示例中,您没有在回调中返回任何内容( return MY_DATAmodels.author.findOne回调中返回,因此什么也没有发生),因此它的行为不符合您的预期。

model.author.findOne is asynchronous model.author.findOne是异步的

model.author.findOne is asynchronous, so you can't expect to call it multiple times in the callback without handling them asynchronously. model.author.findOne是异步的,因此您不能期望在回调中多次调用它而不进行异步处理。

// This code will return an empty array
models.book.find( function (err, books) {
  var i = 0, results = [];

  while (books[i]) {
    models.author.findOne({ "_id": books[i].author_id}, function (err, data) {
      // This will get called long after results is returned
      results.push(data);
    });

    i++;
  };

  return results; // Returns an empty array
});

Handling multiple promises 处理多个承诺

Mongoose uses mpromise , and I don't see a method to handle multiple promises together, but here's a way your case could be done. Mongoose使用mpromise ,但我没有看到一种可同时处理多个promise的方法,但这是可以完成您的案件的一种方法。

var Promise = require('mpromise');

models.book.find().exec()
  .then( function (books) {
    var i = 0,
        count = 0,
        promise = new Promise(),
        results = [];

    while (books[i]) {
      models.author.findOne({ "_id": books[i].author_id }, function (err, author) {
        results.push(author);
        count++;
        // Keep doing this until you get to the last one
        if (count === books.length) {
          // Fulfill the promise to get to the next then
          return promise.fulfill(results);
        }
        return;
      });
    }

    return promise;
  })
  .then( function (results) {
    // Do something with results
  });

I don't know if this will work exactly like it is, but it should give you an idea of what needs to be done. 我不知道这是否能像它一样正常工作,但是它应该使您知道需要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM