第一次调用未将对象传递给端点

时间:2018-12-11 17:06:45

标签: node.js express mongoose

我有一个API,我在其中使用Node / Express / Mongoose时,在这里我试图调用一个函数以获取许多字段的ID来填充我的主要函数,从而为特定用户创建了个人资料卡。

我正在将'owner_id'传递给我的Profile方法。然后,我想在我的主要功能(即Cards文档)中引用返回的字段。

但是当我尝试引用第一个调用的结果时,出现错误消息,指出字段未定义。

这是我的代码:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
    <div class="row">
    
    <div class="col-md">
        <div class="container">
            <div class="row justify-content-md-center">
    
                <div class="position-fixed h-100 col-md-auto d-flex align-items-center">
                    <div class="text-center vertical-center">
                        <h2>Title</h2>
                        <p>Subtext</p>
                        <a data-toggle="modal" data-target="#Modal" href="#">Click me</a>
                    </div>
                </div>
    
            </div>
        </div>
      </div>
    
    </div>
    </div>

任何反馈将不胜感激。

1 个答案:

答案 0 :(得分:1)

一般问题

Profile.findOne是一个异步调用。这基本上意味着需要一些时间来获取配置文件。获取配置文件后,它将传递给作为第二个参数传递的回调函数,并执行该回调函数。

Profile.findOne()调用之后立即执行的所有操作都会在回调函数之前 执行。这是javascript的一个非常普遍的问题,您可以对此in this really famous SO Q/A有所了解。

解决方案

以您为例,您只需将所有需要profile的代码移至回调函数即可:

Profile.findOne({'owner_id': req.body.ownerId}, (err, profile) => {
  //this is the body of the callback function. It will be called some time after `findOne` is called.
  var card = new Card();

  card.owner_id = req.body.ownerId;
  card.card_name = req.body.card_name;
  card.card_subtitle = req.body.card_subtitle;
  card.card_type = req.body.card_type;
  card.card_color = req.body.card_color;

  card.first_name.item = req.body.first_name ;
  card.first_name._id = profile.first_name._id;

  card.last_name.item = req.body.last_name;
  card.last_name._id = profile.last_name._id;

  card.company = req.body.company;
  card.title = req.body.title;

  card.initial.item = req.body.initial ;
  card.initial._id = profile.initial._id;

  card.birthday.item = req.body.birthday ;
  card.birthday._id = profile.birthday._id;

  card.highschool.item = req.body.highschool;
  card.highschool._id = profile.highschool._id;

  card.college.item = req.body.college ;
  card.college._id = profile.college._id;

  card.facebook.item = req.body.facebook;
  card.facebook._id = profile.facebook._id;

  card.linkedin.item = req.body.linkedin ;
  card.linkedin._id = profile.linkedin._id;

  card.linkedin_bus.item = req.body.linkedin_bus ;
  card.linkedin_bus._id = profile.linkedin_bus._id;

  card.twitter.item = req.body.twitter ;
  card.twitter._id = profile.twitter._id;

  card.google.item = req.body.google ;
  card.google._id = profile.google._id;

  card.pinterest.item = req.body.pinterest;
  card.pinterest._id = profile.pinterest._id;

  card.user_image.item = req.body.user_image;
  card.user_image._id = profile.user_image._id;


  card.phones = req.body.phones;
  card.emails = req.body.emails;
  card.addresses = req.body.addresses;
  card.businesses = req.body.businesses;


  card.save(function(err){
    if(err)
      res.send(err);
    res.send({message:'Card has been added'});
  });
});
console.log(profile); // now, however, this line is executed immediately. At this point, profile is not even
// retrieved from the database. Even if the database call was somehow instant, here `profile` is not even in the scope.