【问题标题】:i want to call the node/nest api in crafter cms我想在crafter cms中调用节点/嵌套api
【发布时间】:2022-11-03 14:10:36
【问题描述】:
我试图在crafter cms 中调用nest/node api,但它抛出错误
1)这是我的 Groovy 控制器代码
import groovy.json.JsonSlurper
def apiUrl = "http://localhost:3000/"
def json = new URL(apiUrl).text //assuming it's json
def object = new JsonSlurper().parseText(json)
templateModel.myObject
2)这是我的 ftl 代码
<#import "/templates/system/common/crafter.ftl" as crafter />
<#assign x = contentModel.name_s!"" />
<@crafter.h1>${myObject.x}<@crafter.h1 />
3)保存上述代码后,我收到此错误
!(https://i.stack.imgur.com/XQb7f.png)
【问题讨论】:
标签:
node.js
nestjs
crafter-cms
【解决方案1】:
回复是在http://localhost:3000/以下 URL JSON?
如果没有,尝试解析它会引发错误。
此外,在您的 Freemarker 模板中,您有一个错误:
<@crafter.h1>${myObject.x}<@crafter.h1 />
由于您将 contentModel.name_s 分配给 x,这应该只是:
<@crafter.h1>${x}<@crafter.h1 />
假设来自 Node 的响应是 JSON,并且为了示例,假设它看起来像这样:
{ mySum: 42, myMessage: "Hello World", theBest: "CrafterCMS" }
然后你可以在你的 freemarker 模板中使用这些值:
${myObject.mySum}
myObject 在模板中作为 myObject 可用,因为您已将其添加到控制器中的模板中。
【解决方案2】:
这是我的 JSON 数据:
{
"_id": "632833ecf89459939bd47dd5",
"name": "Random Blood Sugar",
"price": 350,
"description": "Random Blood SugarTimely screening can help you in avoiding illnesses and with Healthians Random Blood Sugar you can keep a track of your health in the best way because Healthians is India's leading health test @ home service with more than 20 lac satisfied customers across 140+ cities.",
"imgURL": "https://i0.wp.com/post.healthline.com/wp-content/uploads/2021/10/blood-test-sample-tube-1296-728-header.jpg?w=1155&h=1528",
"__v": 0
},
这是我的 ftl 代码:
<#import "/templates/system/common/crafter.ftl" as crafter />
<!doctype html>
<html lang="en">
<head>
<#-- Insert your head markup here -->
<@crafter.head />
</head>
<body>
<@crafter.body_top />
<#-- Insert your body markup here -->
<@crafter.h1 $field="demo_s">${myObject.price}</@crafter.h1>
<@crafter.body_bottom />
</body>
</html>
但我遇到了同样的错误。