0

I'm using i18n-node in my express app and i need to get data from database with getLocale() "prefix"

updated question

My mongoose Schema

 var mongoose = require('mongoose'),
   Schema = mongoose.Schema; 
   var ArticleSchema = new Schema({
      title: {
        en : {type: String},
        it : {type: String},
        fr : {type: String},                 
          },
    content: {
        en : {type: String},
        it : {type: String},
        fr : {type: String},                 
          },

MyController

  exports.list = function (req, res) {
       var locale = i18n.getLocale();
       console.log('locale',locale); //working -> en

     Article.find({'title.locale':locale}).sort('-created').populate('user',    'displayName').exec(function (err, articles) {
  if (err) {
    return res.status(400).send({
      message: errorHandler.getErrorMessage(err)
    });
  } else {
    console.log('articles',articles,'res');
    res.json(articles);
  }
});
};

I want to get all articles with en(e.g) prefix. getLocale() method returns a locale like en,it etc. How can i build a query "Find all articles where "title (or content) : getLocale()" ? Even Article.find({'title': 'en'}) not working

Slip
  • 939
  • 6
  • 17
  • 40
  • The return value of `getLocale()` is something like `{en: 'titleA'}` or `{it: 'itaB', en: 'titleB'}`? – zangw Mar 14 '16 at 06:40
  • Possible duplicate of [Using a variable for a key in a JavaScript object literal](http://stackoverflow.com/questions/2274242/using-a-variable-for-a-key-in-a-javascript-object-literal) – Blakes Seven Mar 14 '16 at 06:56

1 Answers1

0

I believe it's hard to fetch a subdocument using its key field alone.You need to provide a value of the subdocument eg Article.find({'title.en' : 'sample' }) . [Update]

I hope this will fix your problem

var locale = getLocale();//get locale  returns it, en
var query = {
  'title.'+locale:{ $exists : true, $ne : null }
}
Article.find(query, function(err, articles) {
  console.log(articles);
});
Devendiran Kumar
  • 223
  • 3
  • 12