Open
Description
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.