gpt4 book ai didi

javascript - React 中未找到 Meteor 方法 (404)

转载 作者:行者123 更新时间:2023-12-03 04:21:10 25 4
gpt4 key购买 nike

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

我有一个组件,我从中调用 Meteor 方法,我只是先做一些测试,看看一切是否按预期工作(当我使用 Blaze 时它工作),但我得到了(除了预期的失败)未找到方法“[meteor 方法名称]”,在 Meteor 方法调用后立即出现 404

错误:

---------------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 方法:

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

最佳答案

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

关于javascript - React 中未找到 Meteor 方法 (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43941131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com