简体   繁体   中英

Meteor 404 Error "Method 'messages.insert' not found"

I've been successfully calling Meteor methods until I created a new Mongo collection. Both collections are found under /imports/collections/ , so I know it's available to both client and server.

Here is the Meteor.method, which is pretty much the same as my working collection.:

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:

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:

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

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

/imports/startup/lib/index.js

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

/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:

// 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 )

Shout out to @MasterAM for helping me find my typo. Turns out I just had an incorrect path in /server/main.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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