首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Sanity发布文档?

如何使用Sanity发布文档?
EN

Stack Overflow用户
提问于 2022-10-25 10:51:21
回答 1查看 31关注 0票数 0

我在一个应用程序中使用了智能cms,我在db上存储了一些文档。我应该从我的前端使用在数据库中创建另一个文档的查询吗?

EN

回答 1

Stack Overflow用户

发布于 2022-11-01 14:08:36

代码语言:javascript
复制
//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);

请查看此视频以获得更多信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74192927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档