【问题标题】:Error while instanciating Restivus "Cannot find name 'Restivus' "实例化 Restivus 时出错“找不到名称‘Restivus’”
【发布时间】:2017-02-08 16:23:31
【问题描述】:

我使用以下方式导入了restivus:

流星加敏捷:restivus

在使用 Restivus 时,我在流星启动时遇到此错误:

“找不到名称‘Restivus’”。

虽然可以 GET 请求,但我想知道它是否会影响应用程序的行为。

这里是使用的代码:

 if (Meteor.isServer) {
        // Global API configuration
        var Api = new Restivus({
            apiPath: 'api/',
            prettyJson: true
        });
    }

收到 POST 时,我的 request.body 和 bodyParams 为空:

Api.addRoute(':id/test', {
        post: function () {
            var id = this.urlParams.id;
            console.log("Body contains : ");
            console.log(this.bodyParams);
            return {
              status: 'success',
              url  : 'post test from id: '+id,
              body : this.bodyParams
            };
        }
    });

有谁知道如何使这个错误消失,如果这与 POST 正文问题有关?

【问题讨论】:

  • 你是如何导入restivus的?使用 npm 或 restivus 包?
  • 用流星添加灵活:restivus

标签: meteor restivus


【解决方案1】:

如果您使用 Meteor 1.4+,您可以尝试将 Restivus 导入到您的文件中,如下所示:

import Restivus from 'nibmle:restivus';

【讨论】:

  • 我使用的是流星 1.6,如果我把这个导入我得到错误:“找不到模块 'nimble:restivus”。
  • 你可以试试meteor/nimble:restivus
【解决方案2】:

帖子正文为空的问题是由我提出的请求引起的: 我没有指定 Content-type 标头。

一旦我指定了“Content-Type”:“application/json”,它就起作用了。

“找不到‘Restivus’”错误仍然存​​在。

【讨论】:

    【解决方案3】:

    您的代码看起来不错。这是我正在使用的仅服务器文件中的一些代码:

    // Global API configuration
      var Api = new Restivus({
        useDefaultAuth: true,
        prettyJson: true,
        apiPath: 'restAPI/',
        defaultHeaders: { 'Content-Type': 'application/json;encoding="UTF-8"' }
      });
    
      // Generates: GET, POST on /api/items and GET, PUT, DELETE on
      // /api/items/:id for the Items collection
      Api.addCollection(Policy);
    

    也许您应该将代码移动到服务器目录?我在 Meteor 1.3.4 上。

    【讨论】:

    • 我已经把代码放到了服务器目录下,我在 Meteor 1.6 上。
    • 流星 1.6?不知道这是否已经发布了,伙计!
    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多