Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironmen


#1

I am calling AWS service in meteor to get imageId. After successfully get image id I want to insert in Mongocollection but my insert code is not working ant it gives following error
Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment. ## here is my code ##

 var ec2 = new AWS.EC2();
          var params = {
             DryRun: false,
             Owners: [
                '12345678'
             ]
          };
             ec2.describeImages(params, function(err, data) {
              if (err)
              { 
                    console.log(err); // an error occurred
              }
              else     
              {  
                  var strData= JSON.stringify(data)
               
                    var Images =  strData.Images
                    var jsonData = JSON.parse(strData);
                 
                    var arrImages = jsonData.Images;
               
                    var _ImageId=arrImages[0].ImageId;
                     console.log("AMI IMAGE  is "+_ImageId)
                  
                      CreateInstense.insert({
                     
                      AMIIMAGE:_ImageId,
                      InstenceId:"instanceId"
                     });       
                    });

#2

You should wrap all non-Meteor (NPM packages for example) callbacks into the Fiber:

const bound = Meteor.bindEnvironment((callback) => {callback();});

// Then in your code
ec2.describeImages(params, function (err, data) {
  bound(() => {
    if (err) {
      console.log(err); // an error occurred
    } else {
      /*..*/
    }
  });
});

#3

Thank you so much! It works


#4

Man! . This works .can you please explain why the way you wrote the code ??


#5

@engrpeters

Meteor uses so-called “coroutines” to parallelize incoming requests in separate synchronous-like threads, this is implemented via fibers NPM package. Read more about Fibers (coroutines) in Meteor:


#6

since
const bound = Meteor.bindEnvironment((callback) => {callback()})

why is this work

io.on('connection', function (client) {
   bound(function(){
        console.log('yh')
        let geoData = HTTP.get('http://ip-api.com/json/' + '208.80.152.201')
        console.log(geoData)
        let cl = {
            id: returnCookie('BHBID', client.request.headers.cookie)[0]
            , lastConnection: {
                timestamp: Date.now()
                , ip_address: client.handshake.address
                , location: ''
            }
            , userAgent: client.handshake.headers['user-agent']
            , online: true
            , //     lastSeen:null
        }
})})

and this dont


io.on('connection', function (client) {
  Meteor.bindEnvironment((callback) => {callback()})(function(){
        console.log('yh')
        let geoData = HTTP.get('http://ip-api.com/json/' + '208.80.152.201')
        console.log(geoData)
        let cl = {
            id: returnCookie('BHBID', client.request.headers.cookie)[0]
            , lastConnection: {
                timestamp: Date.now()
                , ip_address: client.handshake.address
                , location: ''
            }
            , userAgent: client.handshake.headers['user-agent']
            , online: true
            , //     lastSeen:null
        }
    
    })
})

#7

I doubt you’ll undrstand me . :cry::cold_sweat:


#8

Have you tried this way?:

io.on('connection', function (client) {
  Meteor.bindEnvironment(function(){
        console.log('yh')
        let geoData = HTTP.get('http://ip-api.com/json/' + '208.80.152.201')
        console.log(geoData)
        let cl = {
            id: returnCookie('BHBID', client.request.headers.cookie)[0]
            , lastConnection: {
                timestamp: Date.now()
                , ip_address: client.handshake.address
                , location: ''
            }
            , userAgent: client.handshake.headers['user-agent']
            , online: true
            , //     lastSeen:null
        }
    
    })
})

#9

No,It doesn’t .Don’t worry) I will use the first one , which you proposed)


#10
io.on('connection', Meteor.bindEnvironment((client)=>{
        console.log('yh')
        let geoData = HTTP.get('http://ip-api.com/json/' + '208.80.152.201')
        console.log(geoData)
        let cl = {
            id: returnCookie('BHBID', client.request.headers.cookie)[0]
            , lastConnection: {
                timestamp: Date.now()
                , ip_address: client.handshake.address
                , location: ''
            }
            , userAgent: client.handshake.headers['user-agent']
            , online: true
            , //     lastSeen:null
        }
    
  

Easy .