2016-11-24 208 views
1

我試圖從客戶端組件內部調用Meteor.method:流星方法,試圖調用child_process.spawn,得到類型錯誤:child_process.spawn不是一個函數

Meteor.call('execute', this.parameter); 

Meteor.methods有一個函數其派生的過程如下:

cp.spawn(pathtoscript, ['-t', parameter.myid], options); 

這是成功執行有效的處理菌種(它需要30秒來完成),然而瀏覽器控制檯吐出呼叫後立即一個錯誤:

Exception while simulating the effect of invoking 'execute' TypeError: cp.spawn is not a function(…) TypeError: cp.spawn is not a function

我試過只是產卵過程和退出功能,我也試圖等待'關閉'事件。在後端執行兩次都是成功的,但瀏覽器控制檯會拋出異常。

我也試圖撥打Meteor.methods異步

Meteor.call('execute', this.parameter, function(error, result) { 
    if (error) { 
     alert(error, error.reason); 
    } 
     console.log(result); 
    });*/ 

雖然Meteor.methods添加返回值。它總是以同樣的方式結束。

你可以請建議在這種情況下產卵過程的正確方法?

回答

0

這是因爲您的方法代碼在客戶端和服務器端。它不能在客戶端上運行,因爲瀏覽器中沒有spawn

要解決這個問題,你可以簡單地將你的方法服務器代碼只或只是條件Meteor.isServer包裝它的if語句中:

if (Meteor.isServer) { 
    Meteor.methods({ 
    execute(params) { 
     //... 
    } 
    }); 
} 
+0

謝謝,它的工作。它看起來有點令人困惑,因爲我需要從客戶端調用此代碼並在服務器上執行,並且我無法在流星文檔中找到明確的信息。 –

+0

您應該檢查https://guide.meteor.com/structure.html#files-outside – Khang