我已经使用Instagram API构建了一个Web应用程序。我的节点服务器上的主文件“ server.js”文件中包含以下代码。问题在于,“文本”值通常为“空”(无论Instagram用户何时不为其图像添加标题)。此null值使我的应用程序崩溃。我将如何改进此代码以解决这种情况?我的想法是为“文本”提供默认值,当“文本”为null时将插入该默认值。
//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
});
使用一元条件首先检查标题是否存在
text: image.data[0].caption ? image.data[0].caption.text : 'No caption'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句