js-data mongodb adapter only returning _id from findAll()

42 views Asked by At

I'm using js-data with the mongodb adapter js-data-mongodb. I'm running into an issue where store.findAll is only returning the _id of the record. No other fields are returned.

Here is my record in my artworks collection in mongoDB:

_id: ObjectId("59bb7ee069d99027f5219667")
uid :"599b73c285b13252e7f54161"
title: "t1"
description: "t1"
imageUrl: "this.artwork.imageUrl"
videoUrl: "this.artwork.videoUrl"

Part of my js-data store definition:

// Create a store to hold your Mappers
const store = new Container({});

// Mappers in "store" will use the MongoDB adapter by default
store.registerAdapter('mongodb', adapter, { 'default': true });

store.defineMapper('artwork', {
  collection: 'artworks',
  schema: artworkSchema,
  idAttribute: '_id'
});

And the code in my app:

store.findAll('artwork', {uid: '599b73c285b13252e7f54161'}).then((records) => {
    console.log(records);
  })

The output: [ Record { _id: '59bb7ee069d99027f5219667' } ]

What am I missing in order to get all the fields of the record returned in the response?

Thanks!

Update:

Turns out if I remove my schema definition from the mapper, the fields are returned correctly. This is my schema definition. I'm not sure where I'm going wrong here.

const artworkSchema = new Schema({
  $schema: 'http://json-schema.org/draft-06/schema#',
  title: 'Artwork',
  description: 'Schema for Artwork records',
  type: 'object',
  properties: {
    uid: { type: 'string' },
    title: { type: 'string' },
    description: { type: 'string' },
    imageUrl: { type: 'string' },
    videoUrl: { type: 'string' }
  },
  required: ['uid', 'title', 'imageUrl', 'videoUrl']
});
1

There are 1 answers

0
ramkumarshankar On

Argh, that was silly of me. The findAll() call was returning an array of Records, with its attributes. The console.log() wasn't showing it but I am able to get the data by using the array index (records[0].title) or using get('title') as described in the documentation.