[英]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';
因此,對於方法,您可能需要在以下位置建立結構:
import '../imports/startup/lib';
import './methods';
// any other shared code you need to import
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.