意外的令牌:Express和nodejs

时间:2014-01-08 14:36:08

标签: javascript node.js express

尝试加载脚本时出现以下错误:

name:this.name,online 57

a busy cat

var redis = require('redis');
var db = redis.createClient();

module.exports = Card;
function Card(obj)
{
for(var key in obj)
{
    this[key] = obj[key];
}
}

Card.prototype.save = function(fn)
{
if(this.id)
{
    this.update(fn);
}
else
{
    var card = this;
    db.incr('card:ids', function(err, id)
    {
        if(err) return fn(err);
        card.id = id;
        user.update(fn);
    });
}
};

Card.prototype.update = function(fn)
{
var card = this;
var id      = card.id;
var name    = card.name;
var type    = card.type;
var manaCost = card.manaCost;
var attackPoints = card.att;
var magicPoints = card.magic;
var soulPoints = card.soul;
var cardImage = card.cardImage;
db.set('card:id:' + card.name, id, function(err) 
{
    if (err) return fn(err);
    db.hmset('card:' + id, name, type, manaCost, attackPoints, magicPoints, soulPoints, cardImage, function(err) 
    {
        fn(err);
    });
});
   };

   Card.prototype.toJSON = function()
    {
return 
{
    id: this.id,
    name: this.name,
    type: this.type,
    manaCost: this.manaCost,
    attackPoints: this.attackPoints,
    magicPoints: this.magicPoints,
    soulPoints: this soulPoints,
    cardImage: this.cardImage

}
};

var aquaDragon = new Card({
name: 'Aqua Dragon',
type: 'Rain',
manaCost: '6',
attackPoints: '4',
magicPoints: '5',
soulPoints: '6',
cardImage: '/images/Monsters/Aqua%20Dragon.png'
});

var fireElemental = new Card({
name: 'Fire Elemental',
type: 'Sun',
manaCost: '2',
attackPoints: '0',
magicPoints: '2',
soulPoints: '2'
cardImage: '/images/Monsters/Fire%20Elemental.png'
});

aquaDragon.save(function(err){
if (err) throw err;
console.log('user id %d', aquaDragon.id);
});

fireElemental.save(function(err){
if (err) throw err;
console.log('user id %d', fireElemental.id);
});

问题出在.toJSON方法中。使用名称时:在我的其他脚本中使用时,它无法找到问题

3 个答案:

答案 0 :(得分:5)

您正在处理自动分号插入:

而不是

return // ; a semicolon is automatically inserted here
{

使用

return {

了解更多:

答案 1 :(得分:1)

不要像这样写return

return 
{
    id: this.id,

代替

return {
    id: this.id,

因为Java Script通过插入分号来完成return语句。

答案 2 :(得分:0)

{应与return语句位于同一行,因为JS解释器在同一行的return语句后插入一个。

我强烈建议您完全停止使用分号并了解ASI的工作原理,因为如果没有它,您偶尔会遇到这些错误......