Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.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_Express_Mongoose - Fatal编程技术网

Javascript 将返回的结果导出到另一个文件

Javascript 将返回的结果导出到另一个文件,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,如何将下面api的返回结果调用到另一个js文件中的其他函数?我知道这个问题是重复的,但没有返回结果。因此,在标记为副本之前,请先考虑 router.post("/:projectId/events/enriched", checkAuth, (req, res, next) => { const enrichedEvent = new EnrichedEvent({ _id: mongoose.Types.ObjectId(), name: req.

如何将下面api的返回结果调用到另一个js文件中的其他函数?我知道这个问题是重复的,但没有返回结果。因此,在标记为副本之前,请先考虑

router.post("/:projectId/events/enriched", checkAuth, (req, res, next) => {
    const enrichedEvent = new EnrichedEvent({
        _id: mongoose.Types.ObjectId(),
        name: req.body.name,
        projectId: req.params.projectId, //taking from url
        description: req.body.description,
        regex: req.body.regex ,
        kafkaOptions: req.body.kafkaOptions     
    });
    return enrichedEvent.save()  // need all the saved data in another file 
        .then(result => {
            console.log(result);
            res.status(201).json({
                message: "Event stored",
                createdEvent: {
                    _id: result._id,
                    projectId: result.projectId,
                    name: result.name,
                    description: result.description,
                    kafka: result.kafkaOptions,
                    filters: result.filters,
                    regex: result.regex,
                }
            });

            return Project.findOneAndUpdate({
                _id: result.projectId
            }, {
                $push: {
                    enrichedEvents: result._id
                }
            })
        })
       .catch(err => {
            console.log(err);
            res.status(500).json({
                error: err
            });
        });

});

如果我正确理解了您的问题,您是否希望从另一个JS文件中获得
return enrichedEvent.save()…
的结果,而不必调用HTTP API

如果是这样,我建议您从API中提取逻辑。你可以这样做:

// do-something.js
module.exports = function doSomething(projectId, options, onSave) {
  const enrichedEvent = new EnrichedEvent({
    _id: mongoose.Types.ObjectId(),
    name: options.name,
    projectId: projectId,
    description: options.description,
    regex: options.regex,
    kafkaOptions: options.kafkaOptions
  });

  return enrichedEvent
    .save()
    .then(result => {
      onSave(result);

      return Project.findOneAndUpdate(
        {
          _id: result.projectId
        },
        {
          $push: {
            enrichedEvents: result._id
          }
        }
      );
    });
}
您可以从API调用它:

const doSomething = require('./do-something');

router.post("/:projectId/events/enriched", checkAuth, (req, res, next) => {
  return doSomething(req.params.projectId, req.body, (result) => {
      console.log(result);
      res.status(201).json({
        message: "Event stored",
        createdEvent: {
          _id: result._id,
          projectId: result.projectId,
          name: result.name,
          description: result.description,
          kafka: result.kafkaOptions,
          filters: result.filters,
          regex: result.regex
        }
      });
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });
});
您可以从任何其他JS文件调用
doSomething
,提供不同的上下文:

const doSomething=require('path/to/do something')


请注意,我对函数签名和内容做了一些快速的设计决策,但这取决于您确定要在何处分离内容,并命名它们。其主要思想是通过去除所有特定于API的内容来提取函数。

options???在dosomething()的第二个参数中,它如何匹配req.body()?请您也写一下如何在其他文件中使用此dosomething()函数。所以我可以接受答案。当然,我更新了答案以说明如何导出函数并从其他文件导入它。在本例中,我将函数放入另一个文件中,但它可以与其他函数一起放入任何文件中。节点中导出/导入的良好参考: