2017-04-08 84 views
2

我想弄清楚如何從xml文件獲取值並將該值輸出到控制檯。無法從函數獲得輸出

const path = require('path'); 
const fs = require('fs'); 
const parseString = require('xml2js').parseString; 

const sourceFile = path.join(__dirname, 'books.xml'); 

const document = (sourceFile, callback) => { 
    fs.readFile(sourceFile, { encoding: 'utf-8' }, function (err, data) { 
    if (err) { 
     return err; 
    } 

    parseString(data, (err, result) => { 
     result.catalog.book.forEach(users => { 
     callback(users.email); 
     }); 
    }); 
    }); 
}; 

console.log(document(sourceFile, data => data)); 

XML本身是很簡單的:

<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <email>[email protected]</email> 
    </book> 
</catalog> 

但是我從功能上得到的是undefined。爲什麼會發生?

更新: 我想送返回值nodemailer

transporter.sendMail({ from: '[email protected]', to: document(sourceFile, (data) => data), subject: 'Message', text: 'I hope this message gets sent!', }, (err, info) => { if (err) { console.log('error!'); console.log(err.message); } else { console.log('sucess!'); console.log(info.messageId); console.log(info.response); } 

,但我得到一個錯誤: 「缺少必需的頭‘要’。」

回答

1

您不打印數據值,而是打印返回值document函數(它不返回任何內容,因此undefined)。

我已經修改了代碼,所以現在按你們的要求,將工作,

const path = require('path'); 
const fs = require('fs'); 
const parseString = require('xml2js').parseString; 

const sourceFile = path.join(__dirname, 'books.xml'); 

const document = (sourceFile, callback) => { 
    fs.readFile(sourceFile, { encoding: 'utf-8' }, function (err, data) { 
    if (err) { 
     return err; 
    } 

    parseString(data, (err, result) => { 
     result.catalog.book.forEach(users => { 
     callback(users.email); 
     }); 
    }); 
    }); 
}; 

document(sourceFile, data => console.log(data)); 

更新:由於每個操作的評論,

const path = require('path'); 
const fs = require('fs'); 
const parseString = require('xml2js').parseString; 

const sourceFile = path.join(__dirname, 'books.xml'); 

const document = (sourceFile, callback) => { 
    fs.readFile(sourceFile, { encoding: 'utf-8' }, function (err, data) { 
    if (err) { 
     return err; 
    } 

    parseString(data, (err, result) => { 
     result.catalog.book.forEach(users => { 
     callback(users.email); 
     }); 
    }); 
    }); 
}; 

document(sourceFile, data => { 
    transporter.sendMail({ from: '[email protected]', to: data, subject: 'Message', text: 'I hope this message gets sent!', }, (err, info) => { 
    if (err) { 
     console.log('error!'); 
     console.log(err.message); 
    } else { 
     console.log('sucess!'); 
     console.log(info.messageId); 
     console.log(info.response); 
    } 
    }); 
}); 
+0

我想送返回值nodemailer 'transporter.sendMail({ 從: '[email protected]', 於:文件(的資源文件,(數據)=>數據), 主題: '消息', 文本:'我希望這封郵件得到發送!',( },(err,info)=> { if(err){console.log('error!'); console.log(err.message); } else { console.log('sucess!'); console.log(info.messageId); console.log(info.response); } ' 但這是錯誤我geting intead: '缺少必需的標題'到'。' –

+0

@KarolisArbačiauskas,請爲這個問題添加一個相同的更新,將更新我的答案。所以問題答案保持同步。 –

+0

@KarolisArbačiauskas我已添加更新補丁來回答。 –