如何获取基于id的所有文档?

时间:2017-02-10 16:03:46

标签: javascript mongodb mongoose

我从客户端获得价值。假设createdByts768n现在我想找到该用户创建的所有文档并将其发送给客户端,我没有看到任何错误服务器端,但我在客户端有500。如何获取createdBy ts768n的所有文档并将其发送给客户? findByIdAsync似乎无法正常工作。

templateModel.js

var User = require('../user/user.model.js');

var TemplateSchema = new mongoose.Schema({
  name: String,
  id: String,
  appliesTo: [],
  properties: [],
  createdBy: { type: String, ref: 'User' }
});

export default mongoose.model('Template', TemplateSchema);

templateCtrl.js

export function show(req, res) {
  eTemplate.findByIdAsync(req.params.createdBy)
    .then(responseWithResult(res))
    .catch(handleError(res));
}


function responseWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

1 个答案:

答案 0 :(得分:1)

你试过一个简单的异步发现吗?

document.addEventListener('DOMContentLoaded', function() {
  function function_name(data) {
    return  {data: ['nombre','telefono'],pie: 'texto'};
  }
  function _data(info) {
    return info;
  }
  (function() {
    var obj = {
      id: 'id',
      nombre: 'pepe',
      telefono: '6691296347'
    };
    var body = document.getElementsByTagName('body')[0];
    var div = document.createElement('div');
    var p  = document.createElement('p');
    var span = document.createElement('span');
    var _p  = document.createElement('p');
    var _span = document.createElement('span');
    var btn = document.createElement('button');
    var p_ = document.createElement('p');
    var span_ = document.createElement('span');
    p_.textContent = 'ID: ';
    div.appendChild(p_);
    p_.appendChild(span_);
    span_.textContent = obj.id;
    body.appendChild(div);
    data = JSON.stringify(_data(obj));
    p.textContent = 'nombre: ';
    div.appendChild(p);
    p.appendChild(span);
    span.textContent = obj.nombre;
    _p.textContent = 'Telefono: ';
    div.appendChild(_p);
    _p.appendChild(_span);
    _span.textContent = obj.telefono;
    div.appendChild(btn);
    btn.textContent = 'button';
    btn.setAttribute("id","id");
    btn.addEventListener('click',function (e) {
      console.log(_data(obj));
    });
  })();
});