Jam*_*s P 3 mongoose mongodb node.js express instagram
如果我附加任何额外的验证方法,我在保存运行for循环的项目时遇到问题.基本上,我正在构建一个Instagram API应用程序,允许编辑删除不合适的照片.照片从Instagram中分批抽出20张并显示给编辑.如果编辑器点击了照片,它首先按ID放在"黑名单"数据库中,然后从主照片数据库中删除.
为了不将列入黑名单的照片重新出现在Feed上,在将项目保存到主照片数据库之前,需要根据黑名单检查照片的Instagram ID.为此,我使用的是Schema方法.
现在的问题是,我只将一张照片保存到数据库中.如果我拿出方法检查,那么我得到全部20.
这是我的主要创建控制器:
exports.create = function(req, res) {
var topic = req.body.topic || 'nyc';
var path = 'https://api.instagram.com/v1/tags/' + topic + '/media/recent?client_id=' + 'XXXXXXXXXX';
request.get({url: path}, function(err, response){
if (err){
console.log('Failed to get data: ', err);
return res.status(400).json({error: 'Not allowed'});
}
else{
// ------------------------------------------------
// Make sure we have JSON
//
var body = JSON.parse(response.body);
// ------------------------------------------------
// Loop through each response
//
for (var i = 0; i < body.data.length; i++ ){
var photoData = body.data[i];
// ------------------------------------------------
// If there is no caption, skip it
//
if (!photoData.caption){
text = '';
}
else{
text = photoData.caption;
}
// ------------------------------------------------
// Create new photo object
//
var photo = new Photo({
link: photoData.link,
username: photoData.user.username,
profilePicture: photoData.user.profile_picture,
imageThumbnail: photoData.images.thumbnail.url,
imageFullsize: photoData.images.standard_resolution.url,
caption: text,
userId: photoData.user.id,
date: photoData.created_time,
_id: photoData.id
});
photo.checkBlacklist(function(err, blacklist){
if (!blacklist){
photo.save(function(err, item){
if (err){
console.log(err);
}
console.log('Saved', item);
})
}
});
// -------------------------------------------------
//
// Save
//
// -------------------------------------------------
} // END FOR LOOP
console.log('Photos saved');
return res.json(201, {msg: 'Photos updated'} );
}
});
};
Run Code Online (Sandbox Code Playgroud)
这是我的照片架构:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Blacklist = require('../blacklist/blacklist.model');
var PhotoSchema = new Schema({
created: {type: Date, default: Date.now()},
date: String,
link: String,
username: String,
profilePicture: String,
imageThumbnail: {type: String, unique: true},
imageFullsize: String,
caption: String,
userId: String,
_id: {type: String, unique: true}
});
PhotoSchema.methods.checkBlacklist = function(callback){
return Blacklist.findById(this._id, callback);
};
module.exports = mongoose.model('Photo', PhotoSchema);
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我在创建控制器中获得了所有20个保存的控制台消息:console.log('Saved',item);
但实际上只保存了一张照片.有什么想法吗?
谢谢
当您必须为数组中的项执行相同的异步任务时,请不要使用常规for循环.查看async.each,它更适合您的场景,例如(只是else
代码的一部分):
var body = JSON.parse(response.body);
async.each(body.data, function (photoData, callback) {
// ------------------------------------------------
// If there is no caption, skip it
//
if (!photoData.caption){
text = '';
}
else{
text = photoData.caption;
}
// ------------------------------------------------
// Create new photo object
//
var photo = new Photo({
link: photoData.link,
username: photoData.user.username,
profilePicture: photoData.user.profile_picture,
imageThumbnail: photoData.images.thumbnail.url,
imageFullsize: photoData.images.standard_resolution.url,
caption: text,
userId: photoData.user.id,
date: photoData.created_time,
_id: photoData.id
});
photo.checkBlacklist(function(err, blacklist){
if (!blacklist){
photo.save(function(err, item){
if (err){
console.log(err);
}
console.log('Saved', item);
callback();
});
}
});
}, function (error) {
if (error) res.json(500, {error: error});
console.log('Photos saved');
return res.json(201, {msg: 'Photos updated'} );
});
Run Code Online (Sandbox Code Playgroud)
别忘了安装
npm install async
Run Code Online (Sandbox Code Playgroud)
并要求async
:
var async = require('async');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6446 次 |
最近记录: |