猫鼬无点样式功能未被调用

时间:2018-10-26 07:57:04

标签: javascript node.js mongoose

在与mongoose一起使用nodejs时遇到问题。

查询集合后,我希望以无点样式处理具有一些高阶功能的文档:

// the 1st higher-order function:
const macdCalculator = strategy => (data) => {
  console.log('==================')
  console.log('process data with strategy');

  return strategy.process(data);
};

// the 2nd higher-order function:
const balanceCalculator = strategy => (data) => {
  console.log('==================')
  console.log('process data with strategy');

  return strategy.anotherProcess(data);
};

// this just a function
const markOperations = (data) => {
  console.log('==================')
  console.log('start mark operation');

  return someProcess(data);
};

// variables that hold the produced function:
let calculateMacd;
let calculateBalance;

// start query, main program
MyCollection.findById(testId)
  .populate('target')
  .populate('strategy')
  .then(({ target, strategy }) => {
    calculateMacd = macdCalculator(strategy);   // <- produce a funciton
    calculateBalance = balanceCalculator(strategy);   // <- produce another function
    return aggregateData(testId, target.dataSet, strategy.interval);  // <- aggregate some data
  })
  .then(calculateMacd)  //  <- this function seems not been called, the data just passed through!
  .then(markOperations)  //  <- this function has been called, but not working due to the previous step is failed.
  .then(calculateBalance)  //  <- this function also not been called, the data just passed through!
  .then(result => MyOtherCollection.insertMany(result))  // wrong result I got
  .then(() => process.send('test done'))

我发现,如果我不使用无点样式,这些函数将被正确调用:

.then(data => calculateMacd(data))   // this is working
.then(data => calculateBalance(data))   // this is working too

我对猫鼬不太熟悉,为什么会这样呢?还有“ markOperations”功能,为什么这种无点样式起作用?

有什么我想念的吗?

我很困惑,请帮忙。谢谢。

0 个答案:

没有答案
相关问题