【发布时间】:2014-05-12 07:56:53
【问题描述】:
我在使用 Shopify API 更新现有元字段时遇到了困难。每次我收到一个错误,建议我该变体已经存在......所以它一定认为我正在尝试创建一个新的(而不是更新)。
我认为这可能是 'put' 和 'post' 的问题 - 所以将我的方法更改为 put,但错误仍然存在。我已经硬连线了所有变量,以便于测试。
我正在与 Cloudinary 合作。我在 Express.js 中使用 https://github.com/sinechris/shopify-node-api
app.post('/upload', function(req, res){
// upload page... assume we have uploaded our image - but have hard-wired a local file in for now
cloudinary.uploader.upload('/Users/Rob/Pictures/testimg.jpg', function(savedImg) {
var imageURL = savedImg.url;
console.log(imageURL)
},
{
public_id: "testimg"
});
// the saved image is returned - so we add it to our updateMetafieldData json object
var updateMetafieldData = {
"variant": {
"id": '253818949',
"metafields": [
{
"key": "variant_image_0",
"value": 'testimg', // whatever the public id of our image is.
"value_type": "string",
"namespace": "variant_image"
}
]
}
}
// and post the result to shopify - then redirect to /getvariants
Shopify.put('/admin/variants/253818949.json', updateMetafieldData, function(data){
// res.redirect('/getvariants')
});
});
【问题讨论】:
-
我已经通过删除元字段来解决这个问题......但理想情况下希望进行更新。