我正在尝试在服务器中渲染我的反应组件,但我遇到了一些问题,我关注了这篇文章
http://augustl.com/blog/2014/jdk8_react_rendering_on_server/
这是我的代码
(def nashorn (.getEngineByName (ScriptEngineManager.) "nashorn"))
(defn <render-react-comp [template-name data]
(let [remove-react-prefix (fn [react-word] (clojure.string/replace react-word #"react-" ""))]
(go
(dlet [component-name (remove-react-prefix template-name)
template (<! (<react-template! component-name))
lib (<! (<get-react-lib))
evaluation-string
(str "React.renderToString(" component-name "(" (write-str data) "))")]
(. nashorn eval "var global = this;")
(. nashorn eval "var console = {}")
(. nashorn eval "console.debug = print;")
(. nashorn eval "console.warn = print;")
(. nashorn eval "console.log = print;")
(. nashorn eval (.toString lib))
(. nashorn eval template)
(. nashorn eval evaluation-string))))) ;;<---fails here!!!
dlet 宏打印这个
所有这些绑定都是字符串...
component-name : MyComponent
template : var MyComponent = React.createClass({
render: function () {
return React.DOM.h1(null, 'Hi, ' + this.props.msg)
}
})
evaluation-string : React.renderToString(MyComponent({"msg":"HI!!!!!"}))
lib : "....react lib here ...a lot of lines"
这是评估字符串行中的错误消息..fails
Exception in thread "async-dispatch-27" java.lang.Error: javax.script.ScriptException: TypeError: Cannot read property "__reactAutoBindMap" from undefined in <eval> at line number 5816
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1148)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.script.ScriptException: TypeError: Cannot read property "__reactAutoBindMap" from undefined in <eval> at line number 5816
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:93)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at debgtest.viewEngine$_LT_render_react_comp$fn__11415$state_machine__6788__auto____11416$fn__11418.invoke(viewEngine.clj:126)
at debgtest.viewEngine$_LT_render_react_comp$fn__11415$state_machine__6788__auto____11416.invoke(viewEngine.clj:111)
at clojure.core.async.impl.ioc_macros$run_state_machine.invoke(ioc_macros.clj:940)
at clojure.core.async.impl.ioc_macros$run_state_machine_wrapped.invoke(ioc_macros.clj:944)
at debgtest.viewEngine$_LT_render_react_comp$fn__11415.invoke(viewEngine.clj:111)
at clojure.lang.AFn.run(AFn.java:22)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
... 2 more
Caused by: <eval>:5816 TypeError: Cannot read property "__reactAutoBindMap" from undefined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:58)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:214)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:186)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:173)
at jdk.nashorn.internal.runtime.Undefined.lookupTypeError(Undefined.java:128)
at jdk.nashorn.internal.runtime.Undefined.lookup(Undefined.java:113)
at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:98)
at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176)
at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:144)
at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232)
at jdk.nashorn.internal.scripts.Script$\^eval\_$7.L:4-1$L:4974$L:5802$L:5803(<eval>:5816)
at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
... 18 more
我正在使用最新的反应版本,如果您知道可能是什么问题,请告诉我,非常感谢大家!...