2014-11-08 39 views
0

我已經構建了一個利用Instagram API的Web應用程序。我在我的'server.js'文件中有下面的代碼,我的節點服務器上的主文件。問題是,'文本'的價值往往是'空'(每當Instagram用戶不標註他們的形象)。這個null值使我的應用程序崩潰。我如何改進這個代碼來解決這種情況? 我的想法是提供'text'爲'text'的默認值,只要'text'爲null就插入。當空值傳遞時Heroku應用程序崩潰

//Save the new object to DB 
Stadium.findOneAndUpdate({object_id: data.object_id}, { $push: {'photos': 
    { img: image.data[0].images.standard_resolution.url, 
    link: image.data[0].link, 
    username: image.data[0].user.username, 
    profile: image.data[0].user.profile_picture, 
    text: image.data[0].caption.text 
    }}}, 
    { safe: true, upsert: false }, 
    function(err, model) { 
    console.log(err); 
    } 
); 

//Send a socket to client with the new image 
newImage({ 
    img: image.data[0].images.standard_resolution.url, 
    link: image.data[0].link, 
    username: image.data[0].user.username, 
    profile: image.data[0].user.profile_picture, 
    text: image.data[0].caption.text 
}); 

回答

1

使用一個一元的條件首先檢查如果字幕甚至存在

text: image.data[0].caption ? image.data[0].caption.text : 'No caption'