繁体   English   中英

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

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

在创建新的 Mongo 集合之前,我已经成功调用了 Meteor 种方法。 collections 都位于/imports/collections/下,所以我知道客户端和服务器都可以使用它。

这是 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');

我是这样称呼它的:

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() 
}

每当我单击并触发handleComposeClick()时,我都会收到此错误:

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

请记住, /imports文件夹中的所有内容都无法使用,除非使用以下语法将其导入:

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

要么:

import '/imports/server/stuff';

因此,对于方法,您可能需要在以下位置建立结构:

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

虽然如果我是您,我会使用经过验证的方法 ,实际上您在其中导入要使用的方法以便使用它,例如:

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

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

使用这种结构,您将改为/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';

请记住,这实际上并没有执行这些方法,只是确保它们在服务器上已定义,因此,当您在客户端上导入这些经过验证的方法并运行它们时,它不会炸毁该方法在服务器端找不到。

在您的服务器端导入“消息”( /server/main.js

向@MasterAM大喊帮助我找到我的错字。 原来我在/server/main.js路径不正确

暂无
暂无

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

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