我正在使用以下函数 - 来自库knex - 返回一个promise:
21:25:15.013 [qtp60187547-24] INFO org.structr.schema.SchemaHelper - New signature created: TestNodeForSchema
21:25:15.153 [qtp60187547-24] INFO org.structr.schema.SchemaHelper - New signature created: _schema/TestNodeForSchema
21:25:15.345 [qtp60187547-24] INFO org.structr.schema.SchemaHelper - New signature created: TestNodeForSchema/_Ui
21:25:18.740 [qtp60187547-24] INFO o.s.schema.compiler.NodeExtender - Successfully compiled 11 dynamic entities: File.java, _FileHelper.java, Folder.java, Group.java, Image.java, _ImageHelper.java, MailTemplate.java, Page.java, TestNodeForSchema.java, User.java, Widget.java
21:25:24.364 [qtp60187547-24] ERROR org.structr.schema.SchemaService - Unable to compile dynamic schema.
org.neo4j.driver.v1.exceptions.ClientException: Transaction timeout. (Overtime: 2 ms).
at org.neo4j.driver.internal.net.SocketResponseHandler.handleFailureMessage(SocketResponseHandler.java:75)
at org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1$Reader.unpackFailureMessage(PackStreamMessageFormatV1.java:457)
at org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1$Reader.read(PackStreamMessageFormatV1.java:418)
at org.neo4j.driver.internal.net.SocketClient.receiveOne(SocketClient.java:176)
at org.neo4j.driver.internal.net.SocketConnection.receiveOne(SocketConnection.java:215)
at org.neo4j.driver.internal.net.ConcurrencyGuardingConnection.receiveOne(ConcurrencyGuardingConnection.java:165)
at org.neo4j.driver.internal.net.pooling.PooledSocketConnection.receiveOne(PooledSocketConnection.java:183)
at org.neo4j.driver.internal.InternalStatementResult.receiveOne(InternalStatementResult.java:335)
at org.neo4j.driver.internal.InternalStatementResult.tryFetchNext(InternalStatementResult.java:325)
at org.neo4j.driver.internal.InternalStatementResult.hasNext(InternalStatementResult.java:193)
at org.structr.bolt.wrapper.StatementResultWrapper.hasNext(StatementResultWrapper.java:68)
at org.structr.core.graph.CreateNodeCommand.createNode(CreateNodeCommand.java:207)
at org.structr.core.graph.CreateNodeCommand.execute(CreateNodeCommand.java:121)
at org.structr.core.graph.CreateNodeCommand.execute(CreateNodeCommand.java:59)
at org.structr.core.app.StructrApp.create(StructrApp.java:136)
at org.structr.core.entity.AbstractSchemaNode$CreateBuiltInSchemaEntities.execute(AbstractSchemaNode.java:187)
at org.structr.core.entity.AbstractSchemaNode.createBuiltInSchemaEntities(AbstractSchemaNode.java:129)
at org.structr.schema.SchemaService.reloadSchema(SchemaService.java:164)
at org.structr.schema.SchemaHelper.reloadSchema(SchemaHelper.java:319)
at org.structr.schema.ReloadSchema.execute(ReloadSchema.java:34)
at org.structr.core.graph.ModificationQueue.doPostProcessing(ModificationQueue.java:128)
at org.structr.core.graph.TransactionCommand.commitTx(TransactionCommand.java:118)
at org.structr.core.graph.Tx.success(Tx.java:70)
at org.structr.rest.servlet.JsonRestServlet.doPost(JsonRestServlet.java:436)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83)
at org.eclipse.jetty.servlets.AsyncGzipFilter.doFilter(AsyncGzipFilter.java:408)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:389)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:497)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:748)
21:25:24.400 [qtp60187547-24] WARN o.s.rest.servlet.JsonRestServlet - Exception in POST
org.neo4j.driver.v1.exceptions.ClientException: Cannot run more statements in this transaction, because previous statements in the transaction has failed and the transaction has been rolled back. Please start a new transaction to run another statement.
at org.neo4j.driver.internal.ExplicitTransaction.ensureNotFailed(ExplicitTransaction.java:222)
at org.neo4j.driver.internal.ExplicitTransaction.run(ExplicitTransaction.java:190)
at org.neo4j.driver.internal.ExplicitTransaction.run(ExplicitTransaction.java:164)
at org.neo4j.driver.internal.ExplicitTransaction.run(ExplicitTransaction.java:177)
at org.structr.bolt.SessionTransaction.getNodes(SessionTransaction.java:247)
at org.structr.bolt.index.CypherNodeIndex.getResult(CypherNodeIndex.java:62)
at org.structr.bolt.index.AbstractCypherIndex.query(AbstractCypherIndex.java:154)
at org.structr.core.graph.search.SearchCommand.doSearch(SearchCommand.java:210)
at org.structr.core.graph.search.SearchCommand.getResult(SearchCommand.java:315)
at org.structr.core.graph.search.SearchCommand.getAsList(SearchCommand.java:321)
at org.structr.schema.SchemaService.calculateHierarchy(SchemaService.java:271)
at org.structr.schema.SchemaService.reloadSchema(SchemaService.java:197)
at org.structr.schema.SchemaHelper.reloadSchema(SchemaHelper.java:319)
at org.structr.schema.ReloadSchema.execute(ReloadSchema.java:34)
at org.structr.core.graph.ModificationQueue.doPostProcessing(ModificationQueue.java:128)
at org.structr.core.graph.TransactionCommand.commitTx(TransactionCommand.java:118)
at org.structr.core.graph.Tx.success(Tx.java:70)
at org.structr.rest.servlet.JsonRestServlet.doPost(JsonRestServlet.java:436)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83)
at org.eclipse.jetty.servlets.AsyncGzipFilter.doFilter(AsyncGzipFilter.java:408)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:389)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:497)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:748)
但是,我收到以下错误消息:
function findById(id) {
knex('posts').select().where('id', id).first().then(data => {
return data
}).catch((err) => console.log(err));
}
const id = 1
console.log("Post with " + id + ": " + service.findById(parseInt(id)))
有什么建议我做错了吗?我是否创建了承诺false的回调?
感谢您的回复
答案 0 :(得分:1)
您无法在javascript中的promise
中返回值。
为什么?
因为promise
是异步的。
程序的执行顺序因此是
console.log("Post with " + id + ": " + undefined) //Because promise haven't return value yet
knex('posts').select().where('id', id).first().then(data => {
return data
}).catch((err) => console.log(err));
你在这里可以做的是在then块中做一些事情。
function findById(id) {
knex('posts').select().where('id', id).first().then(data => {
console.log(data);
}).catch((err) => console.log(err));
}
如果要将逻辑分离到外部,可以传递回调函数:
function findById(id, callback) {
knex('posts').select().where('id', id).first().then(data => {
callback(data)
}).catch((err) => console.log(err));
}
const id = 1
service.findById(parseInt(id), (data)=>{
console.log(data);
})
答案 1 :(得分:1)
我认为这里有两个问题。
首先,您错过了findById
函数的退货声明,否则findById
将始终返回undefined
。
以下是返回的功能。
function findById(id) {
return knex('posts')
.select()
.where('id', id)
.first()
.then(data => {
return data
})
.catch((err) => console.log(err));
}
此外,您需要在承诺中使用findById
本身,以便在调用之前知道该值已异步解析。
尝试使用调用findById
的高级调用:
const id = 1
service.findById(parseInt(id))
.then((result) => {
console.log("Post with " + id + ": " + result);
});