【问题标题】:Can’t import a module in MeteorJS无法在 MeteorJS 中导入模块
【发布时间】:2019-12-15 17:28:07
【问题描述】:

在 Meteor 应用程序中,我想要一个在服务器上运行并被客户端调用的密码,所以我想要一个在客户端运行但在服务器端执行其部分代码的 Meteor.method (API 调用)

我无法在 lib 或共享文件夹中导入服务器模块
类似于文档中的这个例子

https://guide.meteor.com/security.html#secret-code

导入 > 服务器 > apicalls.js

export const apicall = function () {}
 // Here I want to make my API calls on the server side

共享的>methods.js

import {apicall} from "../imports/server/apicalls.js"; 
//I want to import the apicall function in a method to run it in the client side

我收到错误

Uncaught Error: Cannot find module ‘…/imports/server/apicalls.js’

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    错误消息是由于您在“shared > methods.js”中的文件也试图由 Meteor build for Client 编译,但正如预期的那样,该构建阻止了您在“imports > server > apicalls.js”中的文件无法用于客户端构建。

    这就是 Meteor 为您提供Meteor.call("myMethodName") 语法客户端的原因:您只需提供一个“魔术”字符串,您不必在客户端实际导入方法代码内容。但请确保它在服务器上声明为Meteor.methods({ "myMethodName": function() {} }),以便服务器知道该做什么。

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2020-06-06
      • 2014-08-09
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2012-11-06
      相关资源
      最近更新 更多