Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't query sub-documents in an array #532

Open
echo66 opened this issue Dec 28, 2018 · 3 comments
Open

Can't query sub-documents in an array #532

echo66 opened this issue Dec 28, 2018 · 3 comments

Comments

@echo66
Copy link

echo66 commented Dec 28, 2018

Description

I was trying to query sub-documents in an array. This is the code I'm running:

var jsData        = require('js-data');
var MemoryAdapter = require('js-data-memory');
jsData.utils.Promise = require('bluebird');


var adapter = new MemoryAdapter();

var container = new jsData.Container({ mapperDefaults: { } });

container.registerAdapter('memory', adapter, { 'default': true });



var LangStringSchema = new jsData.Schema({
	type: 'object', 
	properties: {
		lang: { type: 'string' }, 
		value: { type: 'string' }
	}, 
	required: ['lang', 'value']
});


var FoodCategorySchema = new jsData.Schema({
	type: 'object', 
	properties: {
		names: {
			type: 'array', 
			items: LangStringSchema
		}, 
		parentID: { type: 'string' }, 
	}
});


container.defineMapper('food-categories', {
	schema: FoodCategorySchema, 
	relations: {
		belongsTo: {
			'food-categories': {
				foreignKey: 'parentID', 
				localField: 'parentCategory'
			}
		}, 
		hasMany: {
			'food-categories': {
				foreignKey: 'id', 
				localField: 'childrenCategories'
			}
		}
	}
});


var c1 = container.create('food-categories', { 
	names: [
		{ value: 'Carne', lang: 'pt' }, 
		{ value: 'Meat', lang: 'en' }
	]
});

c1.then(function(cat) {
	container.create('food-categories', {
		names: [
			{ value: 'Frango', lang: 'pt' }, 
			{ value: 'Chicken', lang: 'en' }, 
		], 
		parentID: cat['id']
	});

	container.create('food-categories', {
		names: [
			{ value: 'Vaca', lang: 'pt' }, 
			{ value: 'Cow', lang: 'en' }
		], 
		parentID: cat['id']
	});

	container.create('food-categories', {
		names: [
			{ value: 'Porco', lang: 'pt' }, 
			{ value: 'Pork', lang: 'en' }
		], 
		parentID: cat['id']
	});
});


setTimeout(function() {
	container.findAll('food-categories', {
		where: {
			'names.value': {
				'==': 'Cow'
			}, 
			'names.lang': {
				'==': 'en'
			}
		}
	}).then(function(cats) {
		console.log(cats);
	});
}, 200);

I was expecting the output to be an array containing a single document. But, instead, I got an empty array.

Environment

js-data version:

  • js-data@3.0.5
  • js-data-memory@0.1.4

node or browser version:

  • node.js: v8.10.0

operating system:

  • Ubuntu 18.04 LTS

Steps to reproduce

Use the following package.json:

{
  "name": "test-jsdata",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.3",
    "js-data": "^3.0.5",
    "js-data-memory": "^0.1.4",
    "js-data-rethinkdb": "^3.0.0"
  }
}

The content of index.js is the code snippet I included in the description section.

@crobinson42
Copy link
Member

@echo66
Copy link
Author

echo66 commented Dec 30, 2018

Hey, @crobinson42 !

In that page, there are no examples regarding sub-document queries.

EDIT: In 2.9 docs, there is a reference to nested fields. But I can't access the filter method in a container, right?

@crobinson42
Copy link
Member

So what you're trying to accomplish, at the most basic example, is demonstrated here: https://runkit.com/crobinson42/js-data-532-query-sub-documents

Which is not working as expected :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants