开发者

Node.js: How Do I Pass A Vector To A Function?

开发者 https://www.devze.com 2023-02-26 08:46 出处:网络
Here\'s my problem. Given that I a开发者_StackOverflowm saving data in an array: fetch.on(\'message\', function(msg) {

Here's my problem. Given that I a开发者_StackOverflowm saving data in an array:

fetch.on('message', function(msg) {
        msg.data = '';
        msg.on('data', function(chunk) {
            msg.data += chunk;
        });
        msg.on('end', function() {
            msgCache[msg.id].body = msg.data;
        });
    });
fetch.on('end', function() {
     console.log('Done fetching bodies!');
     cb(undefined, msgCache);

  });

And:

fetch.on('end', function() {
       console.log('Done fetching bodies!');
       cb(undefined, msgCache, msg.id);
 });

Which sends the "msgCache" array to cb().

function(msgs) {
    console.log(msgs);  
}

Which works fine. However, I don't know how to get the msgs[msg.id] in the function and without that it's kind of useless to pass the array.

Is there a way I can access msgs[msg.id] in the last function?

Thanks

Output of console.log(msgs):

{ '9991': 
   { headers: 
      { date: [Object],
        to: [Object],
        from: [Object],
        subject: [Object] },
     body: 'test' },
  '9993': 
   { headers: 
      { date: [Object],
        to: [Object],
        from: [Object],
        subject: [Object] },
     body: 'teste2' } }

In order for this to work, I need to access msgs[9991].body for example.


if you call cb(undefined, msgCache, msg.id); with right msg.id (in's not clear where you define it) then you can declare your cb function like this

function cb(dunnowhat, msgs, id) {...}

or i don't get it.

If your haven't msg.id then maybe for .. in helps

function cb(dunnowhat, msgs) {
  for (id in msgs) {
    console.log(msgs[id]);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消