Uncaught SyntaxError:创建地图时JavaScript中的意外标识符

时间:2017-05-11 02:29:54

标签: javascript jquery jsp el

我正在使用jsp并按以下方式创建JavaScript映射:

trunc

但是我继续收到以下错误

未捕获的SyntaxError:意外的标识符

map = new Object();
<c:forEach items="${companyNames}" var="companyName">
  map[${companyName[1]}] = ${companyName[0]};
</c:forEach> 

当我进入Chrome控制台并看到错误来源时,我在Suisse下看到一条红线,问题是什么?

1 个答案:

答案 0 :(得分:1)

您可能需要将密钥包装在引号中。

类似的东西:

class EagerLoadingMixin:
    @classmethod
    def setup_eager_loading(cls, queryset):
        if hasattr(cls, "SELECT_RELATED"):
            queryset = queryset.select_related(*cls.SELECT_RELATED)
        if hasattr(cls, "PREFETCH_RELATED"):
            queryset = queryset.prefetch_related(*cls.PREFETCH_RELATED)
        return queryset


class RentSerializer(serializers.ModelSerializer, EagerLoadingMixin):
    #owner = serializers.SerializerMethodField()
    timeLaps = serializers.SerializerMethodField()
    #gallery = GallerySerializer(read_only=True, many=True)
    #property_category = CategorySerializer()
    SELECT_RELATED = ['owner',]
    PREFETCH_RELATED = ['gallery', 'property_category', ]
    class Meta:
        model = Rental
        read_only = ('id', 'token', 'created_on', 'modified_on', 'slug', )
        fields = ('__all__')


class Rent(APIView):
    """
    List all the rents if token is not provided else a token specific rent
    """
    serializer_class = RentSerializer
    def get(self, request, token=None, format=None):
        reply={}
        try:
            rents = Rental.objects.all()
            if token:
                rent = Rental.objects.get(token=token)
                reply['data'] = self.serializer_class(rent).data
            else:
                reply['data'] = self.serializer_class(rents, many=True).data
        except Rental.DoesNotExist:
            return error.RequestedResourceNotFound().as_response()
        except:
            return error.UnknownError().as_response()
        else:
            return Response(reply, status.HTTP_200_OK)

我上面的语法可能不是100%正确,但基本上你想要:

    map = new Object();
   <c:forEach items="${companyNames}" var="companyName">
      map["${companyName[1]}"] = ${companyName[0]};
  </c:forEach>