我試圖實現一個照片查看器應用程序,它存儲圖像URL,它屬於集合中的用戶ID。當我嘗試使用Collection.findOne時,它總是返回null。流星收集查找()方法是不確定的,即使其訂閱
var name = Meteor.user().username;
var message = $('.action-textarea').val();
var userId = Meteor.userId();
var file = $("#input-photo").get(0).files[0];
if(file) {
fsFile = new FS.File(file);
var id = PostGeneralImages.insert(fsFile, function(err, result) {
if(err) {
throw new Meteor.Error(err);
} else {
var imageLoc = '/cfs/files/PostImages/' + result._id;
PostImages.insert({
generalId: result._id,
userId: Meteor.userId(),
username: Meteor.user().username,
imageLoc: imageLoc
});
}
});
}
var imagePath = PostImages.findOne({generalId: id._id}).imageLoc;
console.log(imagePath);
imagePath在控制檯上未定義。但是當我在控制檯中嘗試findOne方法時,它成功地返回了所需的對象。我已經發布和訂閱該集合。
您的問題
您試圖在將其插入PostGeneralImages.insert函數的回調函數之前查找記錄。 –