Fetching for Strapi v3 is not working with Strapi v4 - vue.js

With Strapi v3 my code perfectly working:
api.loadSource(async actions => {
const { data } = await axios.get('http://localhost:1337/events')
const collection = actions.addCollection({
typeName: 'Event',
path: '/events/:id'
})
for (const event of data) {
collection.addNode({
id: event.id,
})
}
})
But with update to Strapi v4 I'm getting the error: TypeError: gridsome.server.js: data is not iterable (29:25)
27 | })
28 |
> 29 | for (const event of data) {
| ^
30 | collection.addNode({
31 | id: event.id,
I know, that now Strapi added /api/ to the path and also not showing relations by default. But this not fixing the situation:
const { data } = await axios.get('http://localhost:1337/api/events?populate=')
output when enter in browser http://localhost:1337/api/events/1
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z"
}
},
"meta": {
}
}
When enter http://localhost:1337/api/events/1?populate=*
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z",
"categories": {
"data": [
{
"id": 1,
"attributes": {
"name": "Music",
"createdAt": "2022-06-11T12:44:07.472Z",
"updatedAt": "2022-06-11T12:45:32.721Z",
"publishedAt": "2022-06-11T12:45:32.720Z"
}
}
]
}
}
},
"meta": {
}
}

Related

Shopware 6 API / Product Listing / How to add available color variants for each product on the response

API Request
/store-api/product-listing/{categoryId}
I want each product in the API response to have available color variants. Because I want to show color variants on the product list page. But currently this data is not provided by the shopware 6 api.
I've tried to add this data by using associations but it didnt help.
Request Body
{
"includes": {
"product": ["id", "productNumber", "cover", "options"]
},
"associations": {
"options": {
"media": []
}
}
}
The request body looks to be correct. If the product is a variant then it should have content for options.
To verify try the following request body:
{
"limit": 1,
"includes": {
"product": ["id", "productNumber", "options"],
"property_group_option": ["name", "group"],
"property_group": ["name"]
},
"associations": {
"options": {
"associations": {
"group": []
}
}
},
"filters": [
{
"type": "not",
"operator": "and",
"queries": [
{
"type": "equals",
"field": "parentId",
"value": null
}
]
}
]
}
This should yield a single variant.
Response should look similar to this:
{
"entity": "product",
"total": 1,
"aggregations": [],
"page": 1,
"limit": 1,
"elements": [
{
"productNumber": "10042.1",
"options": [
{
"name": "linen",
"group": {
"name": "textile",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
},
{
"name": "35",
"group": {
"name": "size",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
},
{
"name": "chartreuse",
"group": {
"name": "shirt-color",
"apiAlias": "property_group"
},
"apiAlias": "property_group_option"
}
],
"id": "0002ea44c49c41ecb91c43e7e49e422d",
"apiAlias": "product"
}
],
"states": [],
"apiAlias": "dal_entity_search_result"
}

How to add a specific product variant to the shopping cart

The task is to add a product with selection options to the cart. For example, take product with id:2, which has different color and size options. By clicking on the add product button, the product of the selected option should be added to the cart. Product options are predefined. You can see them in the products array below. At the moment, I have created a cart entity in the global store, where I can add a product without selection options and render it on the cart page.
"products" : [
{
"type": "simple",
"id": 1,
"sku": "s1",
"title": "Product 1",
"regular_price": {
"currency": "USD",
"value": 27.12
},
"image": "/images/1.png",
"brand": 9
},
{
"type": "configurable",
"id": 2,
"sku": "c1",
"title": "Product 2",
"regular_price": {
"currency": "USD",
"value": 54.21
},
"image": "/images/conf/default.png",
"configurable_options": [
{
"attribute_id": 93,
"attribute_code": "color",
"label": "Color",
"values": [
{
"label": "Red",
"value_index": 931,
"value": "#ff0000"
},
{
"label": "Blue",
"value_index": 932,
"value": "#0000ff"
},
{
"label": "Black",
"value_index": 933,
"value": "#000"
}
]
},
{
"attribute_code": "size",
"attribute_id": 144,
"position": 0,
"id": 2,
"label": "Size",
"values": [
{
"label": "M",
"value_index": 1441,
"value": 1
},
{
"label": "L",
"value_index": 1442,
"value": 2
}
]
}
],
"variants": [
{
"attributes": [
{
"code": "color",
"value_index": 931
},
{
"code": "size",
"value_index": 1441
}
],
"product": {
"id": 2001,
"sku": "c1-red-m",
"image": "/image/conf/red.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 931
},
{
"code": "size",
"value_index": 1442
}
],
"product": {
"id": 2002,
"sku": "c1-red-l",
"image": "/image/conf/red.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 932
},
{
"code": "size",
"value_index": 1441
}
],
"product": {
"id": 2003,
"sku": "c1-blue-m",
"image": "/image/conf/blue.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 933
},
{
"code": "size",
"value_index": 1442
}
],
"product": {
"id": 2004,
"sku": "c1-black-l",
"image": "/image/conf/black.png"
}
}
],
"brand": 1
}]
My store:
import {createStore} from 'vuex'
import apiRequests from "#/store/actions/apiRequests";
import commonActions from './actions/actions'
import mutations from './mutations/mutations'
import getters from './getters/getters'
const actions = {...commonActions, ...apiRequests}
export default createStore({
state: {
products: [],
brands: [],
cart: []
},
mutations,
actions,
getters
})
actions:
export default {
ADD_TO_CART({commit}, product) {
commit('SET_CART', product)
},
INCREMENT_CART_ITEM({commit}, index) {
commit('INCREMENT', index)
},
DECREMENT_CART_ITEM({commit}, index) {
commit('DECREMENT', index)
},
DELETE_FROM_CART({commit}, index) {
commit('REMOVE_ITEM_FROM_CART', index)
}
}
mutations:
export default {
SET_PRODUCTS_TO_STATE: (state, products) => {
state.products = products
},
SET_BRANDS_TO_STATE: (state, brands) => {
const includesBrands = {}
for (const brand of brands) {
for (const product of state.products) {
if (product.brand === brand.id) {
includesBrands[brand.id] = brand
}
}
}
state.brands = Object.values(includesBrands)
state.brands.unshift({title: "All brands"})
},
SET_CART: (state, product) => {
if (state.cart.length) {
let isProductExists = false
state.cart.map((item) => {
if (item.id === product.id) {
isProductExists = true
item.quantity++
}
})
if (!isProductExists) {
state.cart.push({...product, quantity: 1})
}
} else {
state.cart.push({...product, quantity: 1})
}
},
REMOVE_ITEM_FROM_CART: (state, index) => {
state.cart.splice(index, 1)
},
INCREMENT: (state, index) => {
state.cart[index].quantity++
},
DECREMENT: (state, index) => {
if (state.cart[index].quantity > 1) {
state.cart[index].quantity--
}
}
}
and getters:
export default {
BRANDS(state) {
return state.brands
},
CART(state) {
return state.cart
},
PRODUCTS(state) {
return state.products = state.products.map((product) => {
const brand = state.brands.find((b) => b.id === product.brand)
return {...product, brandName: brand?.title || 'no brand'}
})
},
}
Now I am faced with the fact that I do not understand how I can add a certain product variant to the cart and draw it on the cart page.

I get "invalid id" when using the new fileDelete to remove a file from the admin page

I've been able to make a script to save a file using the new fileCreate but when I try to delete a file with fileDelete, I can't seems to get the right ID.
mutation fileDelete($fileIds: [ID!]!) {
fileDelete(fileIds: $fileIds) {
userErrors {
field
message
}
deletedFileIds
}
}
I pass the ID from my last image: "gid://shopify/ImageSource/20805776113730", called with a new private app key (which should use the v2021-10)
If anybody got that mutation to work, I would appreciate any help.
Response from Shopify:
{
"data": {
"fileDelete": null
},
"errors": [{
"message": "invalid id",
"locations": [{
"line": 3,
"column": 4
}],
"path": ["fileDelete"]
}],
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 1,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 999,
"restoreRate": 50
}
}
}
}
Here is what's send:
{
"query": "mutation fileDelete($fileIds: [ID!]!) {\r\n\t\t\tfileDelete(fileIds: $fileIds) {\r\n\t\t\t userErrors {\r\n\t\t\t\tfield\r\n\t\t\t\tmessage\r\n\t\t\t }\r\n\t\t\t deletedFileIds\r\n\t\t\t}\r\n\t\t }",
"variables": {
"fileIds": "gid:\/\/shopify\/ImageSource\/20825330909250"
}
}
You need to send the ImageMedia ID:
{
"fileIds": "gid://shopify/MediaImage/20835931816073"
}
To get that ID, you need to send your request like this:
{
files(first: 99, reverse:true){
edges{
cursor
node{
... on MediaImage {
id
mimeType
image {
originalSrc
}
}
__typename
createdAt
fileStatus
preview{
image{
altText
id
transformedSrc
originalSrc
}
status
}
}
}
}
}
Response:
{
"data": {
"files": {
"edges": [
{
"cursor": "xxxxxx",
"node": {
"id": "gid:\/\/shopify\/MediaImage\/xxxx",
"mimeType": "image\/png",
"image": {
"originalSrc": "xxxx"
},
"__typename": "MediaImage",
"createdAt": "2021-10-21T17:39:58Z",
"fileStatus": "READY",
"preview": {
"image": {
"altText": "xxxx",
"id": "gid:\/\/shopify\/ImageSource\/xxxx",
"transformedSrc": "xxxx",
"originalSrc": "xxxx"
},
"status": "READY"
}
}
},
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 398,
"actualQueryCost": 18,
"throttleStatus": {
"maximumAvailable": 1000.0,
"currentlyAvailable": 982,
"restoreRate": 50.0
}
}
}
}

GA4 data api - (not set) in custom dimensions

We are currently using GA4 data API and faced the issue when custom dimensions returns value "(not set)".
We were using the following article to set custom dimension for the session count, but we still receiving "(not set)" values.
Example of request:
{
"dateRanges": [
{
"startDate": "2021-09-01",
"endDate": "2021-09-05"
}
],
"offset": 0,
"limit": 100,
"dimensionFilter": {
"filter": {
"fieldName": "eventName",
"stringFilter": {
"matchType": 1,
"value": "screen_view",
"caseSensitive": true
}
}
},
"dimensions": [
{
"name": "customUser:applicationID"
},
{
"name": "customEvent:ga_session_number"
},
{
"name": "dateHour"
},
{
"name": "platform"
},
{
"name": "sessionSource"
},
{
"name": "sessionMedium"
},
{
"name": "sessionCampaignName"
},
{
"name": "deviceCategory"
}
],
"metrics": [
{
"name": "userEngagementDuration"
}
]
}
Does anybody have any idea why it may happen?

Chai to test JSON API output

How can I use Chai to deep parse JSON API output?
JSON API Output:
{
"data": {
"id": "87zc08spyq69",
"type": "data-collector",
"attributes": {
"sensors": [
{
"metadata": {
"sensor_id": 837490,
"unit": "m",
"unit_description": "meters",
"datum": "WGS84",
"offset": -0.001
},
"data": [
{
"time": "2017-12-31T23:59:59Z",
"value": "14.9",
"quality": 4
},
{
"time": "2017-12-31T22:59:59Z",
"value": "12",
"quality": 1
},
{
"time": "2017-12-31T21:59:59Z",
"value": "10",
"quality": 2
}
]
}
]
}
},
"links": {
"self": "/locations/87zc08spyq69/counter/time-series"
}
}
Working So Far:
const chai = require('chai');
const should = chai.should();
const chaiHttp = require('chai-http');
const chaiThings = require('chai-things');
chai.use(chaiHttp);
chai.use(chaiThings);
uri = '/locations/87zc08spyq69/counter/time-series';
describe('/GET', () => {
it('should get location counter in time-series', done => {
chai.request(server)
.get(uri)
.set('Content-Type', "application/vnd.api+json")
.set('Accept', "application/vnd.api+json")
.end((err, res) => {
if (err) {
console.error(err);
done(err,null);
}
should.not.exist(err);
res.status.should.eql(200);
res.type.should.eql("application/vnd.api+json");
res.body.should.have.property('data').has.property('attributes')
.has.property('sensors')
.to.be.an('array')
.should.contain.an('object');
res.body.should.have.property('links').has.property('self').to.be.eql(uri);
done();
});
});
});
I am unable to test any part of the following array:
"data": [
{
"time": "2017-12-31T23:59:59Z",
"value": "14.9",
"quality": 4
},
{
"time": "2017-12-31T22:59:59Z",
"value": "12",
"quality": 1
},
{
"time": "2017-12-31T21:59:59Z",
"value": "10",
"quality": 2
}
]
I have tried chai-things to "deep" test "data", "data/time", "data/value" but I keep getting errors.
How can I test sensors/data, sensors/data/time, sensors/data/value?
Thanks in advance for your help
Its pretty ugly, and there may be a better way to do it, but this worked for me
res.body.should.have.property('data').have.property('attributes')
.have.property('sensors')
.have.deep.property('[0].data').have.deep.property('[0].time').eql("2017-12-31T23:59:59Z");