NodeJS "identifier starts immediately after numeric literal" 在 jade 页面上使用变量 JSON 对象时

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

我有一个 NodeJS 应用程序,它从 jade 页面上的 MongoDB 发送一个 json 对象。在 jade 页面中,我可以通过变量 ${data} 成功使用 json 对象,除非我在 jade page.Then 中的 javascript 中使用它时出现以下错误:

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

我阅读了很多关于 Whosebug 的文章。但我从中了解到 JSON 属性无法处理数值(这是 mongodb 中的标准标识符)。

但我没有提到变量 json 对象。请你能帮助我理解并解决这个问题。

示例变量JSON对象

{ 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 });


    }

});

玉档

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

Mongoose 架构和模型

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);
};

JSON中的数字必须是小数。

如果要使用十六进制,则必须将其表示为字符串。

字符串必须用引号引起来。

我用以下解决方案解决了这个问题,这不是一个干净漂亮的解决方案,但对我有用

  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++