【问题标题】:Meteor method not found in React (404)在 React (404) 中找不到 Meteor 方法
【发布时间】:2017-10-11 23:22:56
【问题描述】:

我很难尝试将 React 集成到 Meteor 中,并且发生了一些非常奇怪的事情。

我有一个从中调用 Meteor 方法的组件,我只是先做一些测试,看看是否一切都按预期工作(当我使用 Blaze 时它工作)但我得到了(除了预期的失败) Method '[meteor method name]' not found404 在 Meteor 方法调用之后。

错误:

---------------prev-inv
meteor.js?hash=27829e9…:932 Exception while simulating the effect of invoking 'cloudinaryImageContents.insert' _class {error: "validation-error", reason: ""name" is required", details: Array(1), message: ""name" is required [validation-error]", errorType: "Meteor.Error"…} Error
    at new _class (http://localhost:3001/packages/mdg_validation-error.js?hash=c824fee078386259563451e9fa961549731965f5:71:89)
    at throwError (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:10660:17)
    at Validator.validate (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:10691:11)
    at Object.Validators.(anonymous function) [as required] (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:10707:19)
    at ScalarField.validate (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:2411:20)
    at ScalarField.validate (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:2807:33)
    at http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:11584:13
    at catchValidationError (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:11513:7)
    at http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:11582:5
    at arrayEach (http://localhost:3001/packages/jagi_astronomy.js?hash=e399f2032bcf4df1ed57f144d7ebec90df5fdd3f:13376:11)
ImageContentAddPage.jsx:26 ---------------after-inv
ImageContentAddPage.jsx:23 errorClass {error: 404, reason: "Method 'cloudinaryImageContents.insert' not found", details: undefined, message: "Method 'cloudinaryImageContents.insert' not found [404]", errorType: "Meteor.Error"…} undefined
ImageContentAddPage.jsx:24 -----inv

组件:

import React, { Component } from 'react';
import TextField from 'material-ui/TextField';
import RaisedButton from 'material-ui/RaisedButton';

import PageSection from '/imports/ui/components/Misc/PageSection';
import ImageUploaderMiddleware from '/imports/ui/components/Middleware/ImageUploaderMiddleware';
import CreatorMiddleware from '/imports/ui/components/Middleware/CreatorMiddleware';

import '/imports/api/contents/images/CloudinaryImage/methods.js';

export default class ImageContentAddPage extends CreatorMiddleware {
  componentWillMount(){
    this.setState({
      path: '/some/path',
      methodName: 'cloudinaryImageContents.insert',
      data: {}
    });
  }

  handleCreate(){
    console.log('---------------prev-inv');
    Meteor.call( this.state.methodName, this.state.data, function(err, res){
      console.log(err, res);
      console.log('-----inv');
    });
    console.log('---------------after-inv');
  }

  render () {
    return (
      <div>
        <TextField hintText="Image name" floatingLabelText="Name" />
        <TextField hintText="Image description" floatingLabelText="Description" />
        <ImageUploaderMiddleware>
        <RaisedButton label="Upload" primary />
        </ImageUploaderMiddleware>
        <div>
          {this.state.error ? this.state.error.reason : null}
        </div>
        <RaisedButton label="Create" primary onTouchTap={this.handleCreate.bind(this)} />
      </div>
    );
  }
}

流星法:

Meteor.methods({
  'cloudinaryImageContents.insert'(image){
    image = image || {};
    image.userId = this.userId;
    let newImage = new CloudinaryImageContent(image);
    return newImage.save();
  }
});

【问题讨论】:

  • 您确定定义该方法的文件捆绑在您的服务器上吗?
  • Per Meteor 记录我在 /imports 文件夹中使用的所有内容。见meteor.com/tutorials/react/security-with-methods
  • 并不表示是捆绑的(实际上是不自动捆绑的意思)。您应该从/imports 之外的入口点导入它(通常在/server/main.js 文件中)。
  • 确实如此。你是对的。刚刚将方法文件导入到我的服务器入口点,它按预期工作。我之前使用的是顶级 /collections 文件夹,但现在我正在对 /imports 进行重构,这就是它之前被导入的原因,但是一旦我将东西移到 /imports 文件夹,它就停止工作了。谢谢!

标签: javascript reactjs meteor methods


【解决方案1】:

如果您将 /imports 文件夹用于您的集合 API 和方法,请确保将方法文件导入到您的服务器入口点 (@987654323 @),否则你的方法将在服务器端不可见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2017-10-29
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多