Fetching data from API and saving them into Database

I have MS SQL database and I'm using FeathersJS 5 as backend .... I'm little bit confused, because I cant get expected results... I'm fetching data from microsoft and then I want to save them into my database, but everytime I do post request I got nothing.

roomFetcher.js

import fetch from 'node-fetch'

export class roomFetcher {
  constructor(accessToken, app) {
    this.accessToken = accessToken
    this.app = app
  }

  async fetchRooms() {
    try {
      const response = await fetch('https://graph.microsoft.com/beta/places/microsoft.graph.room', {
        method: 'GET',
        headers: {
          Authorization: `TOKEN_IS_HERE`
        }
      })

      if (!response.ok) {
        throw new Error(`Microsoft Graph API request failed with status ${response.status}`)
      }

      const data = await response.json()

   
      const formattedRooms = data.value.map((room) => ({
        room_id: room.id,
        room_name: room.nickname,
        room_mail: room.emailAddress
      }))

      console.log(formattedRooms)


      return formattedRooms
    } catch (error) {
      console.error('Error fetching rooms:', error)
      throw error
    }
  }
}

conference-rooms.class.js

import { KnexService } from '@feathersjs/knex'
// import { roomFetcher } from '../roomFetcher.js'

export class ConferenceRoomsService extends KnexService {
  async create(rooms) {
    const ConferenceRoom = this.Model

    for (const room of rooms) {
      await ConferenceRoom.query().insert({
        room_id: room.id,
        room_name: room.nickname,
        room_mail: room.emailAddress
      })
    }
  }

  async fetchRooms(accessToken) {
    try {
      const roomFetcherInstance = new roomFetcher(accessToken, this.app)
      const rooms = await roomFetcherInstance.fetchRooms()
      await this.create(rooms)
      console.log('====================================')
      console.log(rooms)
      console.log('====================================')
      return rooms
    } catch (error) {
      console.error('Error fetching and saving rooms:', error)
      throw error
    }
  }
}

export const getOptions = (app) => {
  return {
    paginate: app.get('paginate'),
    Model: app.get('mssqlClient'),
    name: 'conference_rooms'
  }
}

conference-rooms.schema.js

// For more information about this file see https://dove.feathersjs.com/guides/cli/service.schemas.html
import { resolve, getValidator, querySyntax } from '@feathersjs/schema'
import { dataValidator, queryValidator } from '../../validators.js'

// Main data model schema
export const conferenceRoomsSchema = {
  $id: 'ConferenceRooms',
  type: 'object',
  additionalProperties: false,
  required: ['room_id', 'room_name', 'room_mail'],
  properties: {
    room_id: { type: 'string' },
    room_name: { type: 'string' },
    room_mail: { type: 'string' }
  }
}
export const conferenceRoomsValidator = getValidator(conferenceRoomsSchema, dataValidator)
export const conferenceRoomsResolver = resolve({})

export const conferenceRoomsExternalResolver = resolve({})

// Schema for creating new data
export const conferenceRoomsDataSchema = {
  $id: 'ConferenceRoomsData',
  type: 'object',
  additionalProperties: false,
  required: ['room_id', 'room_name', 'room_mail'],
  properties: {
    ...conferenceRoomsSchema.properties
  }
}
export const conferenceRoomsDataValidator = getValidator(conferenceRoomsDataSchema, dataValidator)
export const conferenceRoomsDataResolver = resolve({})

// Schema for updating existing data
export const conferenceRoomsPatchSchema = {
  $id: 'ConferenceRoomsPatch',
  type: 'object',
  additionalProperties: false,
  required: [],
  properties: {
    ...conferenceRoomsSchema.properties
  }
}
export const conferenceRoomsPatchValidator = getValidator(conferenceRoomsPatchSchema, dataValidator)
export const conferenceRoomsPatchResolver = resolve({})

// Schema for allowed query properties
export const conferenceRoomsQuerySchema = {
  $id: 'ConferenceRoomsQuery',
  type: 'object',
  additionalProperties: false,
  properties: {
    ...querySyntax(conferenceRoomsSchema.properties)
  }
}
export const conferenceRoomsQueryValidator = getValidator(conferenceRoomsQuerySchema, queryValidator)
export const conferenceRoomsQueryResolver = resolve({})

Im really upset, because when I do post request with example body like below it works...

{
"room_id" : "1234",
"room_mail" : "12341243421",
"room_name" : "gfdsgdsf"

}

Upvotes: -1

Views: 70

Answers (1)

pradeep
pradeep

Reputation: 11

in your create method, you are trying to insert data into the database using the insert method of ConferenceRoom.query(). However, you should use the FeathersJS service's create method to insert data into the database correctly. The create method is designed to work with FeathersJS services and ensures that hooks, validation, and other service-related functionality are applied.

async create(rooms) {
  try {
    const createdRooms = await this.Model.create(rooms);
    return createdRooms;
  } catch (error) {
    console.error('Error creating rooms:', error);
    throw error;
  }
}

this.Model.create(rooms), you are utilizing the FeathersJS service's functionality to insert the data into the database.

Upvotes: 0

Related Questions