Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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 使用异步瀑布填充查询选项_Javascript_Node.js_Mongodb_Mongoose_Async.js - Fatal编程技术网

Javascript 使用异步瀑布填充查询选项

Javascript 使用异步瀑布填充查询选项,javascript,node.js,mongodb,mongoose,async.js,Javascript,Node.js,Mongodb,Mongoose,Async.js,我正在尝试mongoose填充查询选项,但我不知道为什么查询选项不起作用 我有一个用户模式: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const UserSchema = new Schema( { username: { type: String, required: true }, email: { type: String }, name: { type:

我正在尝试mongoose
填充
查询选项,但我不知道为什么查询选项不起作用

我有一个用户模式:

const mongoose = require('mongoose');
const Schema   = mongoose.Schema;

const UserSchema = new Schema(
  {
    username: { type: String, required: true },
    email: { type: String },
    name: { type: String },
    address: { type: String }
  },
  { timestamps: true }
);
module.exports = mongoose.model('User', UserSchema);
和提要模式:

const mongoose = require('mongoose');
const Schema   = mongoose.Schema;

const FeedSchema = new Schema(
  {
    user: { type: Schema.ObjectId, ref: 'User' },
    notes: { type: String, required: true },
    trx_date: { type: Date },
    status: { type: Boolean, Default: true }
  },
  { timestamps: true }
);

FeedSchema.set('toObject', { getters: true  });

module.exports = mongoose.model('Feed', FeedSchema);
我想通过
user
id查找所有
feed
,我使用了
async瀑布
如下代码:

async.waterfall([
  function(callback) {
    User
      .findOne({ 'username': username })
      .exec((err, result) => {
        if (result) {
          callback(null, result);
        } else {
          callback(err);
        }
      });
  },

  function(userid, callback) {

    // find user's feed
    Feed
      .find({})
      // .populate('user', {_id: userid._id})  <== this one also doesn't work
      .populate({
        path: 'user',
        match: { '_id': { $in: userid._id } }
      })
      .exec(callback);
  }
], function(err, docs) {
  if (err) {
    return next(err);
  }

  console.log(docs);
});
async.瀑布([
函数(回调){
使用者
.findOne({'username':username})
.exec((错误,结果)=>{
如果(结果){
回调(null,result);
}否则{
回调(err);
}
});
},
函数(用户标识、回调){
//查找用户源
喂
.find({})

//.populate('user',{u-id:userid.\u-id})当
\u-id
的值是“用户”
属性“甚至填充”之前已存储的值时,不确定为什么要在“填充”之后进行匹配

因此,它实际上只是一个简单的“查询”条件,用于
.find()

async.waterfall([
  (callback) =>
    User.findOne({ 'username': username }).exec(callback),

  (user, callback) => {
    if (!user) callback(new Error('not found'));  // throw here if not found
    // find user's feed
    Feed
      .find({ user: user._id })
      .populate('user')
      .exec(callback);
  }
], function(err, docs) {
  if (err) {
    return next(err);
  }

  console.log(docs);
});
当然要记住,
.findOne()
将返回整个文档,因此您只需要在新查询中使用
\u id
属性。还要注意,初始瀑布函数中的“杂耍”是不必要的。如果出现错误,则它将“快速失败”返回到结束回调,或者在结果不存在的地方传递结果。改为将“not found”Delate传递到下一个方法

当然,这真的没有必要,因为它已经存在了一段时间,您真的应该使用它们:

User.findOne({ "username": username })
 .then( user => Feed.find({ "user": user._id }).populate('user') )
 .then( feeds => /* do something */ )
 .catch(err => /* do something with any error */)
或者在MongoDB支持的地方使用:

User.aggregate([
  { "$match": { "username": username } },
  { "$lookup": {
    "from": Feed.collection.name,
    "localField": "_id",
    "foreignField": "user",
    "as": "feeds"
  }}
]).then( user => /* User with feeds in array /* )
这在输出上有点不同,您可以通过一些操作将其更改为相同的外观,但这应该会给您一个大致的想法


重要的是,通常最好让服务器加入,而不是发出多个请求,这至少会增加延迟。

非常感谢您,它现在可以工作了。正如您所说,我将代码更改为使用承诺。