如何在MarkLogic Grove中从自己的API调用后端MarkLogic API? [英] How do I call the backend MarkLogic API from my own API in MarkLogic Grove?

查看:55
本文介绍了如何在MarkLogic Grove中从自己的API调用后端MarkLogic API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是以下问题的继续.

This question is a continuation of the question below.

我如何在Marklogic Grove的node.js中处理REST API?

我能够实现一个示例 REST API,当使用 Grove 进行身份验证时可以调用该 API,如下所示.

I was able to implement a sample REST API that can be called when authenticated with Grove, as shown below.

Middle-tier/routes/index.js

middle-tier/routes/index.js

const authProvider = require('../../grove-node-server-utils/auth-helper');

router.get('/my-rest-api/bar',(req, res) => {
  const response = {status : "success"};
  authProvider.isAuthenticated(req,res,()=>{
    res.send(response);
  });
});

接下来,我想从my-rest-api内调用MarkLogic API.我通过反复试验写了以下内容,但出现错误.我应该怎么写?

Next, I want to call the MarkLogic API from within the my-rest-api. I wrote the following through trial and error, but an error occurs.How should I write it?

Middle-tier/routes/index.js

middle-tier/routes/index.js

const authProvider = require('../../grove-node-server-utils/auth-helper');
const backend = require('../../grove-node-server-utils/backend');

router.get('/my-rest-api/bar',(req, res) => {
  const myResponse = {status : "success!!"};
  console.log("start");
  const backendOptions = {
    method: 'GET',
    path: '/v1/resources/myMarkLogicAPI?p1=test'
  };

  authProvider.isAuthenticated(req,res,()=>{
    console.log("preprocessing");
    backend.call(req, backendOptions, () => {
      console.log("postprocessing")
      res.send(myResponse);
    });
  });
});

错误消息为:{消息":"TypeError:无法将未定义或null转换为对象"}

The error message is: {"message":"TypeError: Cannot convert undefined or null to object"}

推荐答案

我怀疑代码希望将headers属性用作backendOptions的一部分.通常,我们将后端调用包装在authProvider.getAuth调用中,该调用返回传递给后端调用的身份验证标头.您可能要这样做,以确保允许您访问后端.

I suspect the code is expecting a headers property as part of backendOptions. We typically wrap the backend call in a authProvider.getAuth call that returns an authentication header that we pass through to the backend call. You may want to do the same to be sure you are allowed to access the backend.

也就是说,使用我们新的 DefaultRestRoute ,如上一个问题所述.我也在其中显示了一些示例代码.

That said, it may be simpler to use our new DefaultRestRoute as mentioned in your previous question. I showed some sample code in there as well.

HTH!

这篇关于如何在MarkLogic Grove中从自己的API调用后端MarkLogic API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆