我试图找出你在发布新内容时如何返回所有记录?

时间:2017-02-27 05:50:51

标签: node.js mongodb express mongoose

我有一个API,我希望一次性访问数据库以发布新记录,然后返回符合条件的所有记录。

所以这是我的终点:

#button {
    font-family: "Verdana";
    font-weight: inherit;
    font-size: 3vmin;
    line-height: 14vmin;
    text-align: center;
    display: block;
    border: none;
    background: white;
    width: 15vmin;
    height: 15vmin;
    margin: 2vmin auto;
    border-radius: 10vmin;
    cursor: pointer;
    color: black;
}

.click:active {
  display: block;
  background: transparent;
  border: 0px solid transparent;
  height: 50vh;
  width: 50vh;
  margin: 5vh auto 0;

  transition: height 100ms, border-width 100ms, border-color 100ms;

  animation: rotate;
  animation-delay: 100ms;
  animation-timing-function: reverse;   animation-iteration-count: normal;
  animation-duration: 1s; 
  animation-play-state: running;
  animation-direction: reverse;
}

我想返回所有具有相同" business_id"

的记录

问题是它只返回我刚发布的记录。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我不确定从REST角度返回列表是否合适。我可能会另外打电话来获取清单以保持清洁架构。

但无论如何,要实现你想要的东西,你肯定需要进行另一次数据库调用并检索所需的记录:

product.save(function(err, product){
	if(err) {
		//You could send a status here like this: res.status(400).send(err) here.
		res.send(err);
		return;
	}

	Product.find({business_id: req.body.business_id}, function (err, data) {
		if (err) {
			res.send(err);
			return;
		}

		res.send(data);
	});
});