[英]Meteor: insert only working on client, not persisting in server side database
我試圖在每次用戶執行操作時將文檔插入到“ Activities
集合中。 這是我的設置方法:
這是我在collections / activities.coffee中的 createActivity
Meteor.method:
@Activities = new Mongo.Collection('activities')
Activities.allow
insert: (userId, activity) ->
activity.userId == userId
Meteor.methods
createActivity: (movieId, userId) ->
# if Meteor.isServer
# timer = Stats.createTimer("methods.createActivity")
Activities.insert {
docId: movieId
userId: userId
action: "favorite"
collection: "movies"
createdAt: new Date
}, (error, results) ->
console.log error, results
return
在另一個集合collections / lists.coffee中 ,在這里調用它。 在另一個Meteor.method
中被調用,該事件在單擊事件中被調用
Meteor.methods
toggleFavorited: (movieId) ->
if Meteor.isServer
timer = Stats.createTimer("methods.toggleFavorited")
user = Meteor.user()
return unless user?
favorited = Meteor.call('toggleInList', user.favorites, movieId)
if favorited
Meteor.call 'createActivity', movieId, Meteor.userId() #LOOK HERE !!!!!
Movies.update({mid: movieId}, {$inc: favorites: 1})
else
Movies.update({mid: movieId}, {$inc: favorites: -1})
if Meteor.isServer
timer.stop()
favorited
這是我的出版物和訂閱: server / publications.coffee
Meteor.publish 'activities', ->
return Activities.find()
lib / router.coffee
Router.configure
layoutTemplate: 'layout'
loadingTemplate: 'loading'
notFoundTemplate: 'notFound'
trackPageView: true
waitOn: ->
[
Meteor.subscribe("activities")
]
這是我執行觸發“活動”插入方法的用戶操作時的控制台日志:
但是該文檔並未保留在數據庫中! 這是為什么?
謝謝你的幫助
您是否能夠共享文件夾結構,可能是您的方法定義在流星認為僅特定於客戶端的代碼的某個地方,該代碼可以解釋為什么未在服務器上執行該代碼。
我的項目有以下文件夾:
/client/
/both <-- accessible to both client & server
/server <-- looks like you have this, server only code.
如果不這樣做,則可以嘗試將方法代碼移至/ both / lib文件夾,然后查看它是否在服務器上運行。
您如何定義收藏? 您是否偶然通過了null作為參數?
Activities = new Mongo.Collection(null)
將創建一個未持久保存的本地Collection
Activities = new Mongo.Collection("activities")
將保留在服務器上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.