简体   繁体   English

Meteor 404 错误“找不到方法‘messages.insert’”

[英]Meteor 404 Error "Method 'messages.insert' not found"

I've been successfully calling Meteor methods until I created a new Mongo collection.在创建新的 Mongo 集合之前,我已经成功调用了 Meteor 种方法。 Both collections are found under /imports/collections/ , so I know it's available to both client and server. collections 都位于/imports/collections/下,所以我知道客户端和服务器都可以使用它。

Here is the Meteor.method, which is pretty much the same as my working collection.:这是 Meteor.method,它与我的工作集合几乎相同:

import { Mongo } from 'meteor/mongo';

Meteor.methods({
    'messages.insert': function(data) {
        return Messages.insert({
            otherCollectionId: data.otherCollectionId,
            type: data.type,
            createdAt: new Date(),
            sender: this.userId,
            text: data.text
        });
    }
});

export const Messages = new Mongo.Collection('messages');

Here's how I call it:我是这样称呼它的:

import React from 'react';
import { Messages } from '../../imports/collections/messages';
// other imports

export default class Chat extends React.Component {

// other code    

    handleComposeClick() {
        if (this.refs.text) {
            let data = {
                playlistId: this.props.playlist._id,
                type: 'user',
                text: this.refs.text.value
            };

            Meteor.call('messages.insert', data, (error, playlistId) => {
                if (!error) {
                    this.setState({error: ''});
                    this.refs.text.value = '';
                } else {
                    this.setState({ error });
                    console.log(error);
                }
            });
        }
    }

// render() 
}

Whenever I click and trigger handleComposeClick() , I get this error:每当我单击并触发handleComposeClick()时,我都会收到此错误:

errorClass {error: 404, reason: "Method 'messages.insert' not found", details: undefined, message: "Method 'messages.insert' not found [404]", errorType: "Meteor.Error"}

Remember that anything inside the /imports folder will not work unless it's actually imported, either with this syntax: 请记住, /imports文件夹中的所有内容都无法使用,除非使用以下语法将其导入:

import somethingINeed from '/imports/wherever/stuff';
import { somethingElseINeed } from '/imports/wherever/things';

or: 要么:

import '/imports/server/stuff';

So for methods, you may want to set up a structure where you have the following: 因此,对于方法,您可能需要在以下位置建立结构:

/lib/main.js /lib/main.js

import '../imports/startup/lib';

/imports/startup/lib/index.js /imports/startup/lib/index.js

import './methods';
// any other shared code you need to import

/imports/startup/lib/methods.js /imports/startup/lib/methods.js

import { Meteor } from 'meteor/meteor';
import { Messages } from '/imports/collections/messages'; // don't define your collections in a methods file

Meteor.methods({
    'messages.insert': function(data) {
        return Messages.insert({
            otherCollectionId: data.otherCollectionId,
            type: data.type,
            createdAt: new Date(),
            sender: this.userId,
            text: data.text
        });
    }
});

Though if I were you, I'd use validated methods , where you actually import the method you want to use in order to use it, eg: 虽然如果我是您,我会使用经过验证的方法 ,实际上您在其中导入要使用的方法以便使用它,例如:

import { messagesInsert } from '/imports/common-methods/message-methods';

// code...
messagesInsert.call({ arg1, arg2 }, (error, result) => { /*...*/ });

With this structure, you would instead have /server/main.js import ../imports/startup/server which would import ./methods , which (in my particular project) looks like this: 使用这种结构,您将改为/server/main.js import ../imports/startup/server ,它将导入./methods (在我的特定项目中)如下所示:

// All methods have to be imported here, so they exist on the server
import '../../common-methods/attachment-methods';
import '../../common-methods/comment-methods';
import '../../common-methods/tag-methods';
import '../../common-methods/notification-methods';
import '../../features/Admin/methods/index';
import '../../features/Insights/methods';
import '../../features/Messages/methods';

Keep in mind, this doesn't actually execute the methods, it just makes sure they're defined on the server, so that when you import these validated methods on the client side and run them, it doesn't bomb out saying the method can't be found on the server side. 请记住,这实际上并没有执行这些方法,只是确保它们在服务器上已定义,因此,当您在客户端上导入这些经过验证的方法并运行它们时,它不会炸毁该方法在服务器端找不到。

Import 'Messages' in your server side ( /server/main.js )在您的服务器端导入“消息”( /server/main.js

Shout out to @MasterAM for helping me find my typo. 向@MasterAM大喊帮助我找到我的错字。 Turns out I just had an incorrect path in /server/main.js 原来我在/server/main.js路径不正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM