Erik hoeven
Erik hoeven

Reputation: 1610

NodeJS "identifier starts immediately after numeric literal" when using a variable JSON object on a jade page

I have a NodeJS App which send a json object from MongoDB on the jade page. In the jade page i can use succefully the json object by using the variabele ${data}, except when i am using it in an javascript in the jade page.Then i get the following error:

SyntaxError: identifier starts immediately after numeric literal
_id: 56c75f2730cc57130ea7e1db },{ tagScore: null,

I read a lot of articles on Stackoverflow. But what i learned from it is that JSON attributes can't handle nummeric values (which is a standaard identifier from mongodb).

But i don't mentioned about a variable json object. Please can you help me to understand and fix this problem.

Example variable JSON Object

{ tagScore: null,
  tagCattegory: '',
  lookupValue: 'Autoschade',
  typeBusinessRule: 'Zoekwaarde',
  _id: 56c75f2730cc57130ea7e1db }

Routes.js

    req.app.db.models.BusinessRules.pagedFind({
    filters: filters,
    keys: 'lookupValue  tagCattegory  tagScore  typeBusinessRule',
    limit: req.query.limit,
    page: req.query.page,
    sort: req.query.sort
}, function(err, results) {
    if (err) {
        return next(err);
    }

    if (req.xhr) {
        res.header("Cache-Control", "no-cache, no-store, must-revalidate");
        results.filters = req.query;
        console.log("Results XHR ");
        res.send(results);
        res.json(results);
    }
    else {
        results.filters = req.query;
        //res.json(results);
        console.log("Results No XHR ");
        //console.log(results);
        res.render('BusinessRules/index', { data: results.data });


    }

});

Jade file

            script.
            console.log("Load TestData");
            var businessRulesData =  '{"BusinessRules":[ ' + toString(#{data}) + ']}';

Mongoose schema & model

exports = module.exports = function(app, mongoose) {
var rulesSchema = new mongoose.Schema({
    lookupValue: { type: String, required:true},
    typeBusinessRule: { type: String},
    tagCattegory: { type: String},
    tagScore:     { type: Number},
    creationDate: { type: Date},
    search: [String]
});
rulesSchema.plugin(require('./plugins/pagedFind'));
rulesSchema.index({ lookupValue: 1 });
rulesSchema.index({ tagCattegory: 1 });
rulesSchema.index({ typeBusinessRule: 1 });
rulesSchema.index({ tagScore: 1 });
rulesSchema.index({ creationDate: 1 });
rulesSchema.index({ search: 1 });
rulesSchema.set('autoIndex', (app.get('env') === 'development'));
app.db.model('BusinessRules', rulesSchema);
};

Upvotes: 0

Views: 1320

Answers (2)

Erik hoeven
Erik hoeven

Reputation: 1610

I solved the problem with the following soluton, is not a clean and nice solution but it works for me

  each BusinessRule, i in data
        script.

            if (Counter < #{data.length}) {
                myCurentRecords.push('{"opzoekwaarde": "#{BusinessRule.lookupValue}", "cattegorie": "#{BusinessRule.tagCattegory}", "typeBusinessRule": "#{BusinessRule.typeBusinessRule}", "_id": "#{BusinessRule._id}"},');
            }
            else {
                myCurentRecords.push('{"opzoekwaarde": "#{BusinessRule.lookupValue}", "cattegorie": "#{BusinessRule.tagCattegory}", "typeBusinessRule": "#{BusinessRule.typeBusinessRule}", "_id": "#{BusinessRule._id}"}');
            }
            Counter++

Upvotes: 0

Quentin
Quentin

Reputation: 943548

Numbers in JSON have to be decimals.

If you want to use hexadecimal then you have to represent it as a string.

Strings have to be quoted.

Upvotes: 1

Related Questions