无法从NodeJS中的Cloudinary获取image_id [英] Can't get image_id from Cloudinary in NodeJS

查看:109
本文介绍了无法从NodeJS中的Cloudinary获取image_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js和Express。我只是实现Cloudinary从用户上传图像,并将图像ID存储到我的MySQL数据库。



我尝试了Cloudinary文档中的本教程: http://cloudinary.com/documentation/node_image_upload 。一切正常,除了最后一步,我需要获取 image_id

  var url = require('url')
var url_parts = url.parse (req.body.coverurl,true)
console.log(url_parts)//调试
var query = url_parts.query
console.log(query)//调试

var preloaded_file =新cloudinary.PreloadedFile(query.image_id)
if(preloaded_file.is_valid()){
var image_id = preloaded_file.identifier()
console.log( Img ID: + image_id)
} else {
console.log(无效的上传签名)
}

req.body.coverurl 类似于: image / upload / v1426989769 / f08eodnwt5zqfwb2bgpd.png# f63f1fe77c7f536447e079f3dd10829b5a15d862
url_parts 是:

  {协议:null,
斜线:null,
auth:null,
主机:null,
端口:null,
主机名:null,
哈希:'#f63f1fe77c7f536447e079f3d d10829b5a15d862',
搜索:,
查询:{},
路径名:'image / upload / v1426989769 / f08eodnwt5zqfwb2bgpd.png',
路径:'image / upload / v1426989769 / f08eodnwt5zqfwb2bgpd.png,
href: image / upload / v1426989769 / f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862'}
> 然后...,变量 query 为空。 query.image_id 无效,因此无法获取图像ID,也无法验证签名。



我不知道为什么Cloudinary给这个无效的回调字符串。或者我该如何解决这个问题。



感谢您的帮助。并原谅我的英语不好:)



更新



我的Submit_idea.hbs

 < form action = / submit / idea method = POST> 
{{{imgtag}}}<!-将从以下位置呈现:cloudinary.uploader.image_upload_tag(’coverurl’)->
< button type = submit>保存< / button>
< / form>

在我的路线/submit.js中

  router.get('/ submit / idea',function(req,res,next){
//只需使用一些Cloudinary变量
})

router.post('/ submit / idea',function(req,res,next){
var url来渲染submit_idea.hbs = require('url')
var url_parts = url.parse(req.body.coverurl,true)
var query = url_parts.query

var preloaded_file =新的cloudinary。 PreloadedFile(query.image_id)
if(preloaded_file.is_valid()){
var image_id = preloaded_file.identifier()
console.log( Img ID: + image_id)
} else {
console.log(无效的上传签名)
}

//将image_id添加到mysql
})


解决方案

我认为cloudinary网站中的文档有误或过时。尝试按以下方式验证您的上传。它应该工作。

  router.post('/ submit / idea',function(req,res,next){
/ *删除这些部分,我想这些不再需要了。
var url = require('url')
var url_parts = url.parse(req.body.coverurl,true)
var query = url_parts.query
* /
//直接将Coverurl传递给PreloadedFile方法
var preloaded_file = new cloudinary.PreloadedFile(req.body.coverurl)
if(preloaded_file。 is_valid()){
var image_id = preloaded_file.identifier()
console.log( Img ID: + image_id);
//将image_id添加到mysql
} else {
console.log(无效的上传签名)
}
})


I'm using Node.js and Express. I just implement Cloudinary to upload image from user, and store the image id to my MySQL database.

I tried this tutorial from Cloudinary's documentation: http://cloudinary.com/documentation/node_image_upload. Everything works well, except at the final step, I need to get the image_id.

  var url = require('url')  
  var url_parts = url.parse(req.body.coverurl, true)
  console.log(url_parts) //DEBUG
  var query = url_parts.query
  console.log(query) //DEBUG

  var preloaded_file = new cloudinary.PreloadedFile(query.image_id)
  if (preloaded_file.is_valid()) {
    var image_id = preloaded_file.identifier()
    console.log("Img ID:" + image_id)
  } else {
    console.log("Invalid upload signature")
  }

The req.body.coverurl is something like: image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862 The url_parts is:

{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: '#f63f1fe77c7f536447e079f3dd10829b5a15d862',
  search: '',
  query: {},
  pathname: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png',
  path: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png',
  href: 'image/upload/v1426989769/f08eodnwt5zqfwb2bgpd.png#f63f1fe77c7f536447e079f3dd10829b5a15d862' }

And..., variable query is empty. query.image_id is invalid so can't get the image id, also can't verify the signature.

I don't know why Cloudinary give this invalid callback string. Or how can I solve this problem.

Thank for your help. And forgive me for my bad English :)

Update

my submit_idea.hbs

<form action="/submit/idea" method="POST">
    {{{imgtag}}} <!-- this will render from: cloudinary.uploader.image_upload_tag('coverurl') -->
    <button type="submit">Save</button>
</form>

in my routes/submit.js

router.get('/submit/idea', function(req, res, next){
    // just render the submit_idea.hbs with some Cloudinary variable
})

router.post('/submit/idea', function(req, res, next){
  var url = require('url')  
  var url_parts = url.parse(req.body.coverurl, true)
  var query = url_parts.query

  var preloaded_file = new cloudinary.PreloadedFile(query.image_id)
  if (preloaded_file.is_valid()) {
    var image_id = preloaded_file.identifier()
    console.log("Img ID:" + image_id)
  } else {
    console.log("Invalid upload signature")
  }

  // add image_id to mysql
})

解决方案

I guess the docs in cloudinary site is wrong or outdated. Try verifying your upload as below. It should work.

router.post('/submit/idea', function(req, res, next){
  /*REMOVE THESE PARTS. I guess these are not needed any more. 
    var url = require('url')  
    var url_parts = url.parse(req.body.coverurl, true)
    var query = url_parts.query
   */
  //Directy pass coverurl to PreloadedFile method. 
  var preloaded_file = new cloudinary.PreloadedFile(req.body.coverurl)
  if (preloaded_file.is_valid()) {
    var image_id = preloaded_file.identifier()
    console.log("Img ID:" + image_id);
    // add image_id to mysql
  } else {
    console.log("Invalid upload signature")
  }
})

这篇关于无法从NodeJS中的Cloudinary获取image_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆