Shopify。 用shopify-node-api更新元字段时出现问题

使用Shopify API更新现有的metafield时遇到困难。 每当我收到一个错误,build议我该变种已经存在…所以它一定是想我想创build一个新的(而不是更新)。

我认为这可能是“放”和“后”的问题 – 所以改变了我的方法,但错误仍然存​​在。 我已经硬连接了所有的variables,以便于testing。

我正在和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') }); }); 

我实际上创build了Shopify Node API,刚刚发生在几个月后,但我想我会回答任何其他人。

看看这里的shopify API: https ://docs.shopify.com/api/metafield#update

您可以直接通过对元字段资源执行PUT请求来更新元字段,而不是如下所示的变体:

/admin/metafields/#{id}.json

您当然需要首先知道元字段的ID,以便首先需要调用变体,或者您可以将ID存储在本地数据库中以供参考。