【问题标题】:Collection.insert is not a function - MeteorCollection.insert 不是一个函数 - Meteor
【发布时间】:2017-12-04 12:27:32
【问题描述】:

开发者们好!我正在使用 Meteor.js,这是我的第一次体验 我在文件中创建了集合

 // ./dbs/messages.js

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check'; 

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

并在api点中使用它,像这样调用Messages.insert

// server/mail.js

import Messages from './dbs/messages.js';

Meteor.methods({
 'message.post'(messageText, location){
    Messages.insert({
      messageText: messageText,
      location: location
    });
  }
})

但是当我打电话给'message.post' 时,我得到一个错误

Exception while invoking method 'message.post' TypeError
Messages.insert is not a function

但是,当我评论集合导入并像这样在server/main.js 中声明它时

// import Messages from './dbs/messages.js';
const Messages = new Mongo.Collection('messages');

Meteor.methods({
 'message.post'(messageText, location){
    Messages.insert({
      messageText: messageText,
      location: location
    });
  }
});

在这种情况下,我的Messages.insert 工作正常。

谁有使用 Meteor 的经验 - 你能解释一下是什么原因吗? 谢谢! 我还删除了 autopublishinsecure

【问题讨论】:

  • 很可能是导入问题。您应该将其作为命名导入 (import {Messages} from...) 导入,或者将其导出为默认值。
  • 使用花括号,如import { Messages } from './dbs/messages.js';

标签: meteor collections mongoose


【解决方案1】:

我在一个“公共”空间开始我的收藏。我觉得你做的其实是对的。您可以两次声明集合,一次在客户端,一次在服务器端,或者只在公共文件夹中声明一次。我在许多文档中看到保存这些声明的流行位置是 /imports/api ...这对服务器和客户端都是通用的。

Rgs, 保罗

【讨论】:

    【解决方案2】:

    正如@MasterAM 和@Ankur Soni 所说,您需要使用括号import { Messages } from './dbs/messages.js'; 导入消息 不带括号导入的唯一方法是定义消息,然后像这样导出它export default Messages;

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 2013-10-24
      • 2018-01-10
      • 2018-03-11
      • 2017-04-13
      • 1970-01-01
      相关资源
      最近更新 更多