John Davidson

node.js - What would the JS Equivalent of this JSON Object from PHP be?

0 comments
Message:


I'm trying to post this data to an API endpoint I can't figure out the formula it needs to be to actually pass through, I need it to work with NodeJS (axios) for posting


$product = \Shoppy\Models\Product::create([
'title' => 'My test product',
'price' => 10,
'unlisted' => false,
'type' => 'service',
'currency' => 'EUR',
'stock_warning' => 0,
'quantity' => [
'min' => 1,
'max' => 10
],
'email' => [
'enabled' => false
]
]);

EDITED:
With @Nick's suggestion (in comments):


       let opts = {
method: "PUT",
headers: this.headers,
data: JSON.stringify({
"title": "My test product",
"price": 10,
"unlisted": false,
"type": "service",
"currency": "EUR",
"stock_warning": 0,
"quantity": {
"min": 1,
"max": 10
},
"email": {
"enabled": false
}
}),
url: `${this.base_url}/v1/products/`
};

let res = await axios(opts).catch(function(error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
}
});
return res.data;

returns


{
message: 'The given data was invalid.',
errors: {
title: [ 'The title field is required.' ],
price: [ 'The price field is required.' ],
type: [ 'The type field is required.' ],
'email.enabled': [ 'The email.enabled field is required.' ],
currency: [ 'The currency field is required.' ]
}
}


My Answer:
const product = {
title: 'Your title here',
// add other properties as needed
};

Rate this post

5 of 5 based on 1325 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us