Invalid property value: API create Mutation Sanity.io Reference is string - how to define as reference? - properties

i am building a ecommerce site.
I have trouble adding orderItems to the Array in the Order-Document.
As you can see i am trying to reference the customer-Document and Product-Dokument in my orderItem. i am Posting in an array of objects that looks like:
[
{
productId: '5b79b3f8-6ef8-4c6d-bd85-5dcb14fb836d',
kundenId: 'c3777230-74cd-411b-a455-7fa905c90957',
quant: 1,
name: 'Position 1'
}
]
Can someone help me how i get sanity to understand the strings as _ref ?
My schemas are as follows:
export default {
title: 'Order Item',
name: 'orderItem',
type: 'object',
fields: [
{
title: 'Name',
name: 'name',
type: 'string',
},
{
name: 'kundenId',
title: 'Customer',
type: 'reference',
to: [{ type: 'customer' }],
options: {
disableNew: true,
},
},
{
name: 'productId',
title: 'Product',
type: 'reference',
to: [{ type: 'product' }],
options: {
disableNew: true,
},
},
{
title: 'Quantity',
name: 'quant',
type: 'number',
},
{
title: 'Price',
name: 'price',
type: 'number',
},
],
};
and:
export default {
name: 'order',
title: 'Order',
type: 'document',
fields: [
{
name: 'user',
title: 'User',
type: 'reference',
to: [{ type: 'user' }],
options: {
disableNew: true,
},
},
{
name: 'userName',
title: 'User Name',
type: 'string',
},
{
title: 'Order Items',
name: 'orderItems',
type: 'array',
of: [
{
title: 'Order Item',
type: 'orderItem',
},
],
},
{
title: 'CreatedAt',
name: 'createdAt',
type: 'datetime',
},
],
};

so i solved this by using JSON.parse like so:
productId: JSON.parse(`{"_ref":"${artikel._id}"}`),
Good luck to everyone with the same issue.

Related

How do I limit the amount of document of a schema in Sanity?

For example, i have a schema called 'Blog'. i don't want there to be multiple 'Blog' entries, and only allow one entry.
export default {
name: 'blog',
type: 'document',
title: 'Blog',
fields: [
{
name: 'title',
title: 'Title',
type: 'string',
},
{
name: 'content',
title: 'Content',
type: 'array',
of: [{ type: 'block' }],
},
],
}
according to the doc
The type: 'document' property tells the studio that it should make it possible to make new animal documents.
i tried to remove type: 'document' from the schema, but got an error.

How to filter Sanity.io dataset based on a field on references

How to get all posts by a category slug in GROQ?
You can see that a post is added to one or more categories. I would like to get all posts by a category slug to show the posts on a category page. I am new to Sanity.io's GROQ. All the tutorials I have found on creating a blog with sanity.io and next.js have not covered it to show a category page showing posts from a category.
Post schema:
export default {
name: 'post',
title: 'Post',
type: 'document',
fields: [
{
name: 'title',
title: 'Title',
type: 'string',
},
{
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'title',
maxLength: 96,
},
},
{
name: 'author',
title: 'Author',
type: 'reference',
to: {type: 'author'},
},
{
name: 'mainImage',
title: 'Main image',
type: 'image',
options: {
hotspot: true,
},
},
{
name: 'categories',
title: 'Categories',
type: 'array',
of: [{type: 'reference', to: {type: 'category'}}],
},
{
name: 'publishedAt',
title: 'Published at',
type: 'datetime',
},
{
name: 'body',
title: 'Body',
type: 'blockContent',
},
],
preview: {
select: {
title: 'title',
author: 'author.name',
media: 'mainImage',
},
prepare(selection) {
const {author} = selection
return Object.assign({}, selection, {
subtitle: author && `by ${author}`,
})
},
},
}
I have tried the following:
const query = `*[_type == 'post' && $slug in [categories[]-> {slug}] ]{ _id, title, slug, publishedAt, categories[]-> {title,slug}} | order(publishedAt) [0...10]`;
const posts = await client.fetch(
query,
{ slug }
);
console.log("posts", posts);
I got the solution at Sanity's slack channel. The following code helps to get the posts from a category by it's slug.
`*[_type == 'post' && $slug in categories[]->slug.current ]`;

Filter query result by field value inside array of objects [Sanity.io & GROQ]

I'm trying to find a product variant inside my list of products(on sanity.io using GROQ), to do so, I have the sku of the variant that I want.
The query I'm using is
*[_type == "product" && variants[].sku.current =="kit-kat-wasabi-5" ]
But this query returns an empty array. I'm sure that the sku is correct because if I leave the filter aside, and fetch all I can find it.
I tried replacing the "==" with match, but the result is the same.
my schemas are
procuct
export default {
name: 'product',
title: 'Product',
type: 'document',
fields: [
{
name: 'title',
title: 'Inner Title',
type: 'string'
},
{
title: 'SKU',
name: 'sku',
type: 'slug',
options: {
source: 'title',
maxLength: 96
},
validation: Rule => Rule.required()
},
{
name: 'titleWebsite',
title: 'Title Website',
type: 'localeString'
},
{
name: 'active',
title: 'Active',
type: 'boolean'
},
{
name: 'mainImage',
title: 'Imagem',
type:"image"
},
{
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'title',
maxLength: 96
}
},
{
title: 'Base Price',
name: 'basePrice',
type: 'localeCurrency'
},
{
title: 'Quantidade',
name: 'qty',
type: 'number'
},
/* {
title: 'Default variant',
name: 'defaultProductVariant',
type: 'productVariant'
},*/
{
title: 'Variants',
name: 'variants',
type: 'array',
of: [
{
title: 'Variant',
type: 'productVariant'
}
]
},
{
title: 'Tags',
name: 'tags',
type: 'array',
of: [
{
type: 'string'
}
],
options: {
layout: 'tags'
}
},
{
name: 'vendor',
title: 'Vendor',
type: 'reference',
to: {type: 'vendor'}
},
{
name: 'blurb',
title: 'Blurb',
type: 'localeString'
},
{
name: 'categories',
title: 'Categories',
type: 'array',
of: [
{
type: 'reference',
to: {type: 'category'}
}
]
},
{
name: 'body',
title: 'Body',
type: 'localeBlockContent'
}
],
preview: {
select: {
title: 'title',
manufactor: 'manufactor.title',
media: 'mainImage'
}
}
}
And productVariant
export default {
title: 'Product variant',
name: 'productVariant',
type: 'object',
fields: [
{
title: 'Title',
name: 'title',
type: 'string'
},
{
title: 'Title Website',
name: 'titleWebsite',
type: 'localeString'
},
{
title: 'Weight in grams',
name: 'grams',
type: 'number'
},
{
title: 'Price',
name: 'price',
type: 'localeCurrency'
},
{
title: 'SKU',
name: 'sku',
type: 'slug',
options: {
source: 'title',
maxLength: 96
},
validation: Rule => Rule.required()
},
{
title: 'Taxable',
name: 'taxable',
type: 'boolean'
},
{
name: 'blurb',
title: 'Blurb',
type: 'localeString'
},
{
name: 'images',
title: 'Images',
type: 'array',
of: [
{
type: 'image',
options: {
hotspot: true
}
}
]
},
{
title: 'Quantidade',
name: 'qty',
type: 'number'
},
{
title: 'Bar code',
name: 'barcode',
type: 'barcode'
}
]
}
This is a known bug in GROQ when traversing arrays. This will introduce breaking changes if it were to be fixed in GROQ v1. It will therefore be fixed in GROQ v2.
Here is a bug report explaining the issue: https://github.com/sanity-io/sanity/issues/1557. You can show your interest in this problem here.
There's a working draft for version two here: https://github.com/sanity-io/groq.
Regarding your schema, I would consider to change the type of the sku field to be something else, for example a string, and create a new field for the slug which has its source property set to the SKU to be automatically be generated by that field. Documentation for that can be found here: https://www.sanity.io/docs/slug-type.

TreePanel with an specific TreeStore

I've got the flollowing issue:
I'm building a TreePanel with data of people but I don't know how to define the model of it without defineing : leaf, cls and text attributes. I wan't that "Name" would be the node text of each node .
My model is defined as following:
Ext.define('People', {
extend: 'Ext.data.Model',
fields: [
{name: 'Name', type: 'string'},
{name: 'Surname', type: 'string'},
{name: 'Email', type: 'string'}
{name: 'BirthDate', type: 'string'}
]
});
My TreeStore (for the moment with static data, but it will be load from an ajax call to the server that will return a list of server person model). Obviously I don't want to define leaf, text and cls attributes in my server model:
Ext.create('Ext.data.TreeStore', {
root: {
expanded: true,
children: [
{
"Name":"Juan",
"Surname":"Hoz",
"Email": "user#domain.com",
"BirthDate":"19801205"
},
{
"Name":"Marta",
"Surname":"Hoz",
"Email": "user2#domain.com",
"BirthDate":"19831210"
}
}
});
My TreePanel is defined as following:
Ext.create('Ext.tree.Panel', {
id: 'treePersonId',
store: mystore,
hideHeaders: true,
rootVisible: false,
title: 'Persons',
collapsible: true,
resizable:true
});
Can anyone helps me to find the correct way to do this?
Thank you very much,
Juan
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [{
name: 'Name',
type: 'string'
}, {
name: 'Surname',
type: 'string'
}, {
name: 'Email',
type: 'string'
}, {
name: 'BirthDate',
type: 'string'
}]
});
Ext.require('*');
Ext.onReady(function() {
var store = Ext.create('Ext.data.TreeStore', {
model: 'Person',
root: {
expanded: true,
children: [{
"Name": "Juan",
"Surname": "Hoz",
"Email": "user#domain.com",
"BirthDate": "19801205"
}, {
"Name": "Marta",
"Surname": "Hoz",
"Email": "user2#domain.com",
"BirthDate": "19831210"
}]
}
});
Ext.create('Ext.tree.Panel', {
renderTo: document.body,
store: store,
hideHeaders: true,
rootVisible: false,
columns: [{
xtype: 'treecolumn',
dataIndex: 'Name',
flex: 1
}]
});
});

Clear a datastore and repopulate it

I have the following store:
Ext.define('Sencha.store.AdultMenuStore', {
extend: 'Ext.data.Store',
config: {
onItemDisclosure: true,
model:'Sencha.model.MenuPoint',
data: [
{
id: 'addChild',
name: 'Add child',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-plussign.png',
xtype: 'addchildform'
},{
id: 'share',
name: 'Share',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-shareicon.png',
xtype: 'childmenu'
},{
id: 'myProfile',
name: 'My Profile',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-profile.png',
xtype: 'childmenu'
},{
id: 'help',
name: 'Help',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-question.png',
xtype: 'childmenu'
}]
}
});
Which uses the following model:
Ext.define('Sencha.model.MenuPoint', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'string'},
{name: 'name', type: 'string'},
{name: 'icon_url', type: 'string'},
{name: 'xtype', type: 'string'}
]
}
});
Some places in the code I add menu points dynamically like this:
var child = children[i];
var menuPoint = Ext.create('Sencha.model.MenuPoint', {id: child.childId, name: child.firstName, icon_url: 'aLink', xtype: 'childmenu'});
store.add(menuPoint);
And sometimes I need to clear the store, ergo remove the menu points I added dynamically and only use the menu points I hardcoded in the store. I see methods for removing and adding in the store, but I dont know how to reset the store and repopulate it with the static data I defined there.
do as I have answered on your prev question, which you did not respect :)
var data = [
{
id: 'addChild',
name: 'Add child',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-plussign.png',
xtype: 'addchildform'
},{
id: 'share',
name: 'Share',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-shareicon.png',
xtype: 'childmenu'
},{
id: 'myProfile',
name: 'My Profile',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-profile.png',
xtype: 'childmenu'
},{
id: 'help',
name: 'Help',
icon_url: 'http://kidsparent.no/images/adultMenu/bkids-home-question.png',
xtype: 'childmenu'
}]
store.load(function (store) {
store.add(data)// data is an array with you local data
})
store.load() function cleans up prev data
Cheers, Oleg