【问题标题】:How can I post documents using Sanity?如何使用 Sanity 发布文档?
【发布时间】:2022-11-01 22:09:29
【问题描述】:

我正在为应用程序使用 sanity cms,并且我在 db 上存储了一些文档。我应该使用哪个查询从我的前端在数据库中创建另一个文档?

【问题讨论】:

    标签: api sanity


    【解决方案1】:
    //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);
    

    check this video for more

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2022-11-27
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      相关资源
      最近更新 更多