流星/javascript函数在另一个文件中不起作用

meteor / javascript function not working when in another file

本文关键字:文件 不起作用 另一个 javascript 函数 流星      更新时间:2023-09-26

这很有趣。我有一个这样的文件结构:

/
/client/
/server/

我正在开发的应用程序运行良好,我在/client/文件夹中有很多.js文件,所有文件都分为逻辑部分(对我来说)。它们在编译时运行良好。

不过,我添加了一个名为miscFunctions.js的新文件,并添加了一种简单的函数并保存:

function sessionDataReset(){
  //Set the New Organisation Session data to be false, meaning we're not adding a new organisation
  return Session.set('addingOrganisation', false);
};

调用此函数时返回以下错误:

Uncaught ReferenceError: sessionDataReset is not defined

当我把确切的代码移到.js文件中时,我会从中调用它,它工作得很好。

为什么这个错误会发生,因为我知道我想做的事情可以用流星来完成?

非常感谢任何建议。

Rob

首先尝试以这种方式声明文件:

sessionDataReset = function() {
  //Set the New Organisation Session data to be false, 
  //meaning we're not adding a new organisation
  return Session.set('addingOrganisation', false);
};

这样可以确保该功能在全局范围内可见
(@user1623481 Meteor在编译文件时将其包装为IIFE,从而创建了一个限制该函数可见性的函数范围。)

这很可能会解决问题,但在检查Meteor Docs 中的文件加载顺序之后