9

我是流星和mongo db的新手。我正在使用[email protected]提出申請。我正在創建一個名爲'/imports/api/collections/recipe.js'的文件。在這裏我創建一個集合並將這個文件導入到'/server/main.js'和'/client/main.js'中。在recipe.js中,我只發佈食譜集合,然後在我訂閱的客戶端文件中。直到這一點,一切都是正確的。然後我使用autoform創建一個表單,該表單運行良好並被創建。但這種形式永遠不會發布。流星收集沒有被自動創建在開始和autoform不張貼到mongo分貝

首先,我的假設是,當服務器啓動時,那時我的數據庫應該已經創建了一個名爲recipe的集合,但事實並非如此。

其次,爲什麼autoform不工作?我認爲這是因爲第一個原因。

在客戶端,我通過蒙古(流星玩具)看到我的食譜集合。

我的食譜文件 - '/imports/api/collections/recipe.js':

import { Meteor } from 'meteor/meteor'; 
import { Mongo } from 'meteor/mongo'; 
import { SimpleSchema } from 'meteor/aldeed:simple-schema'; 

var RecipesCollection = new Mongo.Collection('recipes'); 

RecipesCollection.allow({ 
    insert(userId, doc){ 
     return !!userId; 
    } 
}) 

var RecipeSchema = new SimpleSchema({ 
    name: { 
     type: String, 
     label: "Name" 
    }, 
    desc: { 
     type: String, 
     label: "Description" 
    }, 
    author: { 
     type: String, 
     label: "Author", 
     autoValue(){ 
      return this.userId 
     }, 
     autoform:{ 
      type: "hidden" 
     } 
    }, 
    createdAt: { 
     type: Date, 
     label: "Created At", 
     autoValue(){ 
      return new Date(); 
     }, 
     autoform:{ 
      type: "hidden" 
     } 
    } 
}); 

RecipesCollection.attachSchema(RecipeSchema); 

if (Meteor.isServer) { 
    // This code only runs on the server 
    Meteor.publish('recipes', function tasksPublication() { 
    return RecipesCollection.find({author: this.userId}); 
    }); 
} 

export const Recipes = RecipesCollection; 

我的服務器的文件: '/server/main.js':

import { Meteor } from 'meteor/meteor'; 
import '/imports/startup/server/start.js'; 
import '/imports/api/collections/recipe.js'; 

我的客戶js文件:

import { Template } from 'meteor/templating'; 
import { Recipes } from '/imports/api/collections/recipe.js'; 

import '/imports/ui/pages/NewRecipe.html'; 
Template.NewRecipe.onCreated(function bodyOnCreated() { 
    Meteor.subscribe('recipes'); 
}) 
Template.NewRecipe.helpers({ 
    recipesCollection() { 
     return Recipes; 
    } 
}); 

新配方模板:

<template name="NewRecipe"> 
    <div class="new-recipe-container"> 
     {{> quickForm collection=recipesCollection id="insertRecipeForm" type="insert" class="new-recipe-form" }} 
    </div> 
</template> 

我正在使用包:Collection2和autoform。任何幫助或建議,將不勝感激。謝謝

請隨時通過分流我的流星學項目來使其工作。會非常大膽。 - https://github.com/devin6391/meteorLearn1/tree/master/recipebook

回答

1

好的解決....很慚愧的這個,因爲這是再次一個流星包的版本問題 - 'accounts-ui'。由於着名的CDN問題,有時包裝在印度不會升級。

但是,收集不會自行創建。我不得不去mongoDB控制檯並自己創建它。然後只有autoform貼到它

0

爲什麼在/server/main.js中重新導入流星/流星?

要做到這一點我用出口是這樣的:

export const RecipesCollection = new Mongo.Collection('recipes'); 

然後用集合的名稱:

{{> quickForm collection="RecipesCollection" id="insertRecipeForm" type="insert" class="new-recipe-form" }} 
+0

我不重新導入流星/流星。它根據es6模塊系統的不同文件而不同。 此外,我不能直接使用RecipesCollection作爲模塊的變量而不是窗口或模板的變量 –

+0

- 已完成。雖然你對多個流星/流星進口是正確的。我修正了這一點。 RecipesCollection集合不能直接放入quickform中,因爲它不在窗口範圍內。 –