2016-11-22 1 views
0

2週間前に私はcourseraのコースを勉強し始めました。Meteor.isClientメソッドをconsole.logとMongoで使用する方法を示す例があります。しかし、それは動作しませんでした。まず、あなたのconsole.logはそれがあることはかなり正常であり、あなたのMeteor.isServerの外にある私のWindows CLIでも、再起動後に出力に何も表示されませんし、私は、ブラウザのコンソールタイプconsole.log(Images.find().count())にしようとした場合には、0メーターはウィンドウ内で無視されます

Images = new Mongo.Collection("images"); 
if (Meteor.isServer){ 
    Meteor.startUp(function(){ 
    if(Images.find().count() == 0){ 
     Images.insert({ 
     img_src:'1.jpg', 
     img_alt:'Here i am !' 
     }); 
    } //end of if have no images 
    }); 
} 
console.log('startup : ' + Images.find().count()); 

答えて

2

を出力しますそれはクライアントに現れます。

第2に、サーバーにログを表示する場合は、startUp関数またはクライアントで呼び出すメソッドにそれを入れる必要があります。長い時間のために、今、それはあなたが/client/serverディレクトリにコードを分割する必要があるのではなくMeteor.isServerを使用してMeteor.isClientrecommendedしてきたが

0

Meteor.isServerは、ウィンドウで正常に動作します。新しいバージョンの流星で

、あなたがmeteor create testAppを実行し、この作成され、以下:

meteor directory structure

/clientのすべてのコードは唯一のクライアント上で実行され、そして/serverのすべてのコードは、サーバー上でのみ実行されます。

/client/main.js

import { Template } from 'meteor/templating'; 
import { ReactiveVar } from 'meteor/reactive-var'; 

import './main.html'; 

Template.hello.onCreated(function helloOnCreated() { 
    // counter starts at 0 
    this.counter = new ReactiveVar(0); 
}); 

Template.hello.helpers({ 
    counter() { 
    return Template.instance().counter.get(); 
    }, 
}); 

Template.hello.events({ 
    'click button'(event, instance) { 
    // increment the counter when button is clicked 
    instance.counter.set(instance.counter.get() + 1); 
    }, 
}); 

Meteor.startup(() => { 
    console.log('Ping! from /client/main.js - Meteor.startup()') 
}); 

console.log('Ping! from /client/main.js - Top Level') 

/server/main.js

import { Meteor } from 'meteor/meteor'; 

Meteor.startup(() => { 
    console.log('Ping! from /server/main.js - Meteor.startup()') 
}); 

console.log('Ping! from /server/main.js - Top Level') 

これらの初期のファイルは、以下のログコマンドを含めるように編集/作成には、両方の場所(クライアント/サーバー)とのコードを注文する実行されることを示します

/shared.js

ここでは3210
// Runs on Both 
console.log('Hi from /shared.js - Top Level') 
Meteor.startup(() => { 
    console.log('Hi from /shared.js - Meteor.startup()') 
}); 


// Runs on Server 
if(Meteor.isClient){ 
    console.log('Hi from /shared.js - isClient') 
    Meteor.startup(() => { 
    console.log('Hi from /shared.js - isClient, Meteor.startup()') 
    }); 
} 

// Runs on Server 
if(Meteor.isServer){ 
    console.log('Hi from /shared.js - isServer') 
    Meteor.startup(() => { 
    console.log('Hi from /shared.js - isServer, Meteor.startup()') 
    }); 
} 

/lib/shared.js

console.log('Ping! from /lib/shared.js - Top Level') 

Meteor.startup(() => { 
    console.log('Ping! from /lib/shared.js - Meteor.startup()') 
}); 

サーバとブラウザからの結果のログです:クライアントとサーバーの両方で enter image description here

、ないMeteor.startupブロック内console.log行が続く、最初に実行Meteor.startupコールバックは、サーバープロセスの開始が完了するまで、またはDOMの準備が完了するまで遅延されるためです。それらはMeteor.startupへの呼び出しが行われたのと同じ順序で実行されます。

Meteor.startupブロックにないコールは、ファイルがロードされるときに、Default file load orderに続いて実行されます。

関連する問題