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

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"
                     });       
                    });

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 {
      /*..*/
    }
  });
});
8 Likes

Thank you so much! It works

1 Like

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

1 Like

@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:

1 Like

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
        }
    
    })
})
1 Like

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

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

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

1 Like
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 .

2 Likes

image

Please help me out!

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

// Then in your code
function signup(req, res) {
  bound(() => {
      /*..*/
  });
}

async function signin(req, res) {

console.log("Hi");

const email = req.body.email

Users.findOne({email},{limit: 10},(err,user)=>{

// this part is not triggered ?

  if(err || !user){

    return res.status(400).json({

        error:'User With This Email Does Not exist | Signup To Continue '

    })

}

  

  console.log("test");

  const token = jwt.sign({_id: user._id},JWT_SECRET)

  res.cookie('t',token,{expire:new Date()+9999})

  const {_id,name,email,gender} = user

// res.status(201).json({ data: user });

  return res.json({token,user:{_id,email,name,gender}})

});

}

Users.findOne is working without callback why it is not working with callback ?

Because findOne does not take a callback:
Mongo.Collection#findOne([selector], [options])

1 Like