2016-12-03 80 views
1

我正在使用流星,使用打字稿es6的angular2,而我在爲異步功能編寫流星法時遇到了這個錯誤。我在插入查詢中遇到同步問題,因爲當名稱相同時,它在插入時不提供給我一個錯誤。所以我決定使用光纖和未來,但打字稿不斷給我一個錯誤,它無法找到纖維/未來的模塊。 我試圖 流星NPM安裝光纖, 流星NPM安裝的將來, 流星添加ostrio:[email protected] 但沒有什麼工作。如果有任何解決方案,請告訴我。如果有任何其他方法可以用來解決這個問題,請告訴我。流星:在打字稿中寫流星法時找不到模塊光纖/未來

import { Emails } from '../collections/emails.collection'; 
import { check } from 'meteor/check'; 
import { Meteor } from 'meteor/meteor'; 
import Future from 'fibers/future'; 

Meteor.methods({ 
    loadEmailTemp: function(tempn: string){ 
    let temp = Emails.findOne({tempname: tempn}); 
    return temp; 
    }, 
    getAllTemplates: function() { 
    let temps = Emails.find({}).fetch(); 
    return temps; 
    }, 
    newTemplate: function(tempn: string) { 
    let err = false; 

    let result = Emails.insert({ 
     tempname: tempn, 
     subject: '', 
     text: '' 
    }, function(error, result){ 
     if(error){ 
     console.log("ERROR",error); 
     err = true; 
     } 
     if(result) console.log("RESULT",result); 
    }); 
    return result; 
    } 
}); 

我的錯誤是

W20161203-13:15:44.055(5.5)? (STDERR)  /Users/exMachina/.meteor/packages/meteor-tool/.1.4.2_1.1ulueqv++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280 
W20161203-13:15:44.056(5.5)? (STDERR)      throw(ex); 
W20161203-13:15:44.056(5.5)? (STDERR)      ^
W20161203-13:15:44.057(5.5)? (STDERR) 
W20161203-13:15:44.057(5.5)? (STDERR) Error: Cannot find module 'fibers' 
W20161203-13:15:44.058(5.5)? (STDERR)  at Function.Module._resolveFilename (module.js:325:15) 
W20161203-13:15:44.058(5.5)? (STDERR)  at Function.Module._load (module.js:276:25) 
W20161203-13:15:44.058(5.5)? (STDERR)  at Module.require (module.js:353:17) 
W20161203-13:15:44.059(5.5)? (STDERR)  at require (internal/module.js:12:17) 
W20161203-13:15:44.059(5.5)? (STDERR)  at Object.<anonymous> (/Users/exMachina/.meteor/packages/ostrio_neo4jdriver/.1.0.2.1tve9ie++os+web.browser+web.cordova/npm/node_modules/neo4j-fiber/lib/helpers.js:33:11) 
W20161203-13:15:44.059(5.5)? (STDERR)  at Object.<anonymous> (/Users/exMachina/.meteor/packages/ostrio_neo4jdriver/.1.0.2.1tve9ie++os+web.browser+web.cordova/npm/node_modules/neo4j-fiber/lib/helpers.js:62:4) 
W20161203-13:15:44.060(5.5)? (STDERR)  at Module._compile (module.js:409:26) 
W20161203-13:15:44.060(5.5)? (STDERR)  at Object.Module._extensions..js (module.js:416:10) 
W20161203-13:15:44.067(5.5)? (STDERR)  at Module.load (module.js:343:32) 
W20161203-13:15:44.067(5.5)? (STDERR)  at Function.Module._load (module.js:300:12) 

回答

1

我不知道你的代碼錯誤,它看起來不錯。所以我給其他方法的建議:

  • 使用Promise:流星方法與Promise很好地發揮。所以,如果你請求一個REST API,並希望等待結果作進一步行動,承諾將是一個非常適合:

    import fetch from 'node-fetch'; 
    
    Meteor.methods({ 
        getFromApi() { 
        retrun fetch('https://link.to/api-endpoint').then((result) => { 
         // ... 
         return result; // send result to client callback 
        }).catch((error) => { 
         // handle error 
         throw error; 
        }); 
        } 
    }); 
    
  • 流星還提供處理異步操作的方法,它是Meteor.wrapAsync

    import fetch from 'node-fetch'; 
    
    const fetchSync = Meteor.wrapAsync(fetch); 
    
    Meteor.methods({ 
        getFromApi() { 
        const result = fetchSync('https://link.to/api-endpoint'); 
        // ... 
        return result; 
        } 
    });