【问题标题】:public_id is not defined Meteor+Cloudinarypublic_id 未定义 Meteor+Cloudinary
【发布时间】:2017-04-20 22:18:59
【问题描述】:

我收到上述关于使用 Lepozepo 上传图片功能的错误消息:CLoudinary 包和以下详细信息。

  • console.log 显示成功上传和一个文件。
  • 上传后看不到图像。个人资料图像也没有改变。可能是未定义的 public_id,因此无法将其保存到 Meteor.userId()?
  • 进度条不显示

删除按钮错误

更改删除代码后的第三条错误消息

以下代码:

服务器的 config.js

Cloudinary.config({
  cloud_name    : 'XX',
  api_key       : 'XX',
  api_secret    : 'XX'
});
Cloudinary.rules.delete = function() {
  var userId = "my_user_id";
  return this.public_id;
};
Cloudinary.rules.signature = function() { return this.userId;};
Cloudinary.rules.private_resource = function() {return this.userId; };

客户端 - upload.js

Template.upload.onCreated(function(){
   var self = this;
   self.autorun(function(){ 
      self.subscribe('user',  Meteor.userId());    
      $.cloudinary.config({ cloud_name: "XX", api_key: 'XX'}); 
   });
});

Template.upload.events({   
   'submit form': function(e, t) { 
      e.preventDefault(); 
      var files = [];
      var file = $('#userimage')[0].files[0]; 
      files.push(file);
      console.log(files);

     Cloudinary.upload(files, {}, function(err, res) {
       if (err){
         console.log("Error: " + err + ",due to: " + err.reason);
     } else {

       // preview segment 
      $('.cloudinary-fileupload').bind('cloudinarydone', function(e, data) {  
       $('.preview').html(
         $.cloudinary.image(data.result.public_id, { format: data.result.format, version: data.result.version, crop: 'fill', width: 150, height: 100 })
        );    
        $('.image_public_id').val(data.result.public_id);    
        return true;
      });

     // progress bar
     $('.cloudinary-fileupload').bind('fileuploadprogress', function(e, data) { 
       $('.progress_bar').css('width', Math.round((data.loaded * 100.0) /        data.total) + '%'); 
    });

    Meteor.users.update({ _id: Meteor.userId() }, { 
      $set: { 'profile.profilepicId'  : res.public_id }
    });
    console.log("Success :" + res);              
  },
  'click #delete': function (public_id) {
      Cloudinary.delete("response.public_id", public_id, function(err, res){
         if(err) {
            console.log("Error: " + err + ",due to " + err.reason);
         } else { 
            console.log("Success :" + res);
            Meteor.users.update({ _id: Meteor.userId() }, { 
               $set: { 'profile.profilepicId'  : {} }
            });
         }
      });
   }
});

客户端 - upload.html

 <form>
   <input type="file" id="userimage" name="userimage" class='upload_field cloudinary-fileupload' /> <!-- edited as per suggested --> 
     <!-- the below progress bar and thumbnail preview not working --> 
     <div class="progress_bar" style='background-color: red, height: 20px'></div>
     <div class='thumbnails'></div>

       {{#each file}}
          <p> {{percent_uploaded}}% done</p> <!-- works but only shows number --> 
       {{/each}}
     <button id="submit">Upload</button>
     <button id="delete">Delete</button>
 </form>

客户 - 个人资料。

Template.profile.helpers({
   profilepicId: function (){
      var u = Meteor.user();
      return u.profile.profilepicId
   },
   file: function () {
      return Cloudinary.collection.find();
   }
});

客户端 - profile.html

<img src="{{c.url profilepicId format=format crop='thumb' width=200 height=200}}">

【问题讨论】:

  • 能否也添加渲染后的 标签代码?另外,public_id 分配不应该发生在 _upload_file 回调的成功块中吗?
  • @NadavOfir 我添加了输入代码。不知道你说的第二个问题是什么意思。感谢您是否可以详细说明?所以它显示一个注册的文件,然后没有public_id,然后显示[object Object]成功。
  • fileuploadprogress 不会启动,因为元素类 (cloudinary-fileupload) 与实际输入字段的类 (cloudinary_fileupload) 不对应。一旦这对您有用,请尝试绑定 cloudinarydone 并从那里处理响应,即更新您的模型。见:cloudinary.com/blog/…
  • 谢谢,这是我犯的一个愚蠢的错误。我会尝试进行更改。public_id 问题有任何运气吗?
  • public_id is unknown at this stage. you should initialize it either based on res` 在您的 _upload_file() 调用的成功块中,或者使用返回上传结果的cloudinarydone 事件。有意义吗?

标签: javascript html meteor cloudinary


【解决方案1】:

根据我们上面的通信,请尝试以下操作(集合更新移至上传响应处理程序内部):

  Cloudinary._upload_file(files[0], {}, function(err, res) {
     if (err){
        console.log("Error: " , err, err.reason);
        return;
     } else {
        console.log("Success :" + res);
        Meteor.users.update({ _id: Meteor.userId() }, { 
            $set: { 'profile.avatar': res.public_id }
        });
     }
  }); 

希望现在这更有意义...

【讨论】:

  • 嗨 Nadav,上传工作正常,但我使用 Cloudinary.upload(files, {}, function(err, res) { 基于包所有者的新版本更新。但是进度条、预览和删除仍然不起作用。栏不显示和删除只是刷新页面并说res 未定义也许你可以展示我应该如何将它包含在上面的代码中。
  • @Thinkerer 请更新您上面的代码或分享可以复制的实时 URL。不看网络通讯很难说。
  • 更新了行。 2 个问题:[1] 删除不起作用并引发最大调用堆栈错误; [2] 较小的问题,无法让进度条和预览图像正常工作。
  • @Thinkerer [1] 你能分享堆栈跟踪/错误消息吗? [2] 尝试将 progress_bar div 包裹在另一个具有明确宽度的 div 中(例如 &lt;div style="width:500px"&gt;
  • 错误信息图片在第一个错误信息图片的正下方。或者你想要一个扩展版本?错误可能意味着我输入了太大的东西,比如整个集合或其他东西。进度条仍然不显示,也许是我正在使用的包?上传时,该数字确实会跃升至 100%。也许是一个 DIY 进度条..
猜你喜欢
  • 2015-11-06
  • 2015-10-20
  • 2015-12-06
  • 2012-10-09
  • 1970-01-01
  • 2017-05-16
  • 2021-06-19
  • 1970-01-01
  • 2021-04-07
相关资源
最近更新 更多