【发布时间】:2022-11-01 22:09:29
【问题描述】:
我正在为应用程序使用 sanity cms,并且我在 db 上存储了一些文档。我应该使用哪个查询从我的前端在数据库中创建另一个文档?
【问题讨论】:
我正在为应用程序使用 sanity cms,并且我在 db 上存储了一些文档。我应该使用哪个查询从我的前端在数据库中创建另一个文档?
【问题讨论】:
//Create a client file (for example, in scr folder in React) and add where appropriate
//In my case, I have a token and project Id in a.env file
import sanityClient from '@sanity/client';
import imageUrlBulder from '@sanity/image-url';
export const client = sanityClient({
projectId: process.env.REACT_APP_SANITY_PROJECT_ID,
dataset:'production',
apiVersion: '2020-10-21',
useCdn:true,
token: process.env.REACT_APP_SANITY_API_TOKEN,
});
const builder = imageUrlBulder(client);
export const urlFor = (source) => builder.image(source);
//in the other file include these lines
import { client } from '../client';
//these fields should match your schema declaration
const doc = {
_id: "yout_id",
_type: 'your_doc_type',
userName:name,
image: imageUrl,
}
client.createIfNotExists(doc)
.then(() => {
console.log("document created")
})
.catch(console.error);
【讨论】: