簡體   English   中英

MongoDB Create Resolver - 如何返回新創建的文件

[英]MongoDB Create Resolver - How to return the newly created document

我無法讓我的創建解析器返回新創建的文檔。 它為所有值返回 null。 這是代碼:

解析器

    // SECTION SCHEDULE
    addSchedule: async (
      parent,
      {
        _id,
        streetAddress,
        suite,
        city,
        state,
        zip,
        startDate,
        endDate,
        startTime,
        endTime,
        squareFeet,
        jobDetails,
        numberOfClientEmployees,
        client,
        employees,
        isDisplayable,
      },
      context
    ) => {
      // _id, streetAddress, suite, city, state, zip, startDate, endDate, startTime, endTime, squareFeet, jobDetails, numberOfClientEmployees, client, employees
      // if (context.user) {
      const user = await Schedule.create({
        _id,
        streetAddress,
        suite,
        city,
        state,
        zip,
        startDate,
        endDate,
        startTime,
        endTime,
        squareFeet,
        jobDetails,
        numberOfClientEmployees,
        client,
        employees,
        isDisplayable,
      });
      return (
        {
          _id,
          streetAddress,
          suite,
          city,
          state,
          zip,
          startDate,
          endDate,
          startTime,
          endTime,
          squareFeet,
          jobDetails,
          numberOfClientEmployees,
          client,
          employees,
          isDisplayable,
        },
        {
          new: true,
          returnNewDocument: true,
        }
      );
      // }
      // throw new AuthenticationError("You need to be logged in!");
    },

突變

// SECTION SCHEDULE
export const ADD_SCHEDULE = gql`
  mutation AddSchedule(
    $streetAddress: String
    $suite: String
    $city: String
    $state: String
    $zip: String
    $startDate: String
    $endDate: String
    $startTime: String
    $endTime: String
    $squareFeet: String
    $jobDetails: String
    $numberOfClientEmployees: String
    $client: String
    $employees: [String]
    $isDisplayable: Boolean
  ) {
    addSchedule(
      streetAddress: $streetAddress
      suite: $suite
      city: $city
      state: $state
      zip: $zip
      startDate: $startDate
      endDate: $endDate
      startTime: $startTime
      endTime: $endTime
      squareFeet: $squareFeet
      jobDetails: $jobDetails
      numberOfClientEmployees: $numberOfClientEmployees
      employees: $employees
      client: $client
      isDisplayable: $isDisplayable
    ) {
      _id
      streetAddress
      suite
      state
      city
      zip
      startDate
      startTime
      endDate
      endTime
      jobDetails
      numberOfClientEmployees
      squareFeet
      isDisplayable
      client {
        _id
      }
      employees {
        _id
      }
    }
  }
`;

前端執行


  const [mostRecentScheduleAddId, setMostRecentScheduleAddId] = useState(); //fix

  const [addSchedule] = useMutation(ADD_SCHEDULE, {
    onCompleted: (data) => { //fix
      console.log('add schedule = ', data);
      setMostRecentScheduleAddId(data?.addSchedule?._id);
      console.log('most recent id = ', mostRecentScheduleAddId);
    },
    refetchQueries: [
      { query: QUERY_SCHEDULE }, // DocumentNode object parsed with gql
      "getSchedule", // Query name
    ]
  });

控制台結果

add schedule =  
{addSchedule: {…}}
addSchedule
: 
city
: 
null
client
: 
null
employees
: 
null
endDate
: 
null
endTime
: 
null
isDisplayable
: 
null
jobDetails
: 
null
numberOfClientEmployees
: 
null
squareFeet
: 
null
startDate
: 
null
startTime
: 
null
state
: 
null
streetAddress
: 
null
suite
: 
null
zip
: 
null
__typename
: 
"Schedule"
_id
: 
null

我已經查看了文檔。 如上面代碼所述,我嘗試了 new: true 因為這是 findOneAndUpdate 上的方法。 沒有運氣。 目前我查詢在數據庫中創建的最后一個文檔,但我不確定這是一個長期的方法(例如,如果插入順序在路上發生變化)。 但也許這是解決方案。 感謝您的任何見解。

有人用解決方案回答了我的帖子,寫得很好。 我現在在這里看不到它。 我無法完全復制它,但解決方案是將解析器中的返回語句更改為讀取“返回用戶”,以便返回先前塊中創建的結果“用戶”“ const user = await Schedule.create ……”。 非常感謝您發現我的這個錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM