1

我正在使用 typescript es6 和 angular2 一起使用流星,我在为异步函数编写流星方法时遇到了这个错误。我在插入查询中遇到同步问题,因为当名称相同时,它没有向我提供插入错误。所以我决定使用纤维和未来,但打字稿一直给我一个错误,它找不到纤维/未来的模块。我试过 meteor npm install fiber,meteor npm install future,meteor add ostrio:neo4jdriver@1.0.2-fiber ,但没有任何效果。如果有任何解决方案请告诉我。如果有任何其他方法可以用来解决这个问题,请告诉我。

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)
4

1 回答 1

1

我不知道你的代码有什么问题,看起来不错。所以我对其他方法提出建议:

  • 使用 Promise:Meteor 方法可以很好地与 Promise 配合使用。因此,如果您请求 REST api 并希望等待结果以进行进一步操作,Promise 将非常适合:

    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 还提供了一种处理异步操作的方法,它是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;
      }
    });
    
于 2016-12-03T09:03:04.927 回答