我正在express.js上构建一个REST api.我无法更新路线中的变量.
例:
我正在调用app.get(“/ wp / page / create /:id”,function(req,res)
在这条路线中,我首先使用request-promise库调用http请求.我在嵌套的http调用中使用此调用的响应.
我使用全局变量作为嵌套调用的标题,并且它是标题,我需要使用etag变量进行更改.
码:
global.postHeaders = headers;
postHeaders['X-HTTP-Method'] = "MERGE";
postHeaders['Content-Type'] = 'application/json;odata=verbose';
postHeaders['X-RequestDigest'] = spContext;
request.get({
url: "xxx",
headers: headers,
json: true
}).then(function(response) {
var etag = response.d.__metadata.etag
postHeaders['If-Match'] = etag;
request.post({
url: "xxx",
type: "POST",
body: data,
headers: postHeaders,
json: true
}).then(function(data) {
res.send(data).end()
console.log("All done!");
})
})
当我启动服务器并进入路线时,一切正常.当我试图再次点击它时,etag变量仍然是相同的,即使它应该更新.
如果我重新启动服务器,它会在第一次尝试时再次运行,但在第二次/第三次尝试时失败.
知道我做错了什么吗?
解决方法:
我已经解决了这些问题.简单的解决方案是清除包含变量的标头.
global.postHeaders = headers;
postHeaders['X-HTTP-Method'] = "MERGE";
postHeaders['Content-Type'] = 'application/json;odata=verbose';
postHeaders['X-RequestDigest'] = spContext;
request.get({
url: "xxx",
headers: headers,
json: true
}).then(function(response) {
var etag = response.d.__metadata.etag
postHeaders['If-Match'] = etag;
request.post({
url: "xxx",
type: "POST",
body: data,
headers: postHeaders,
json: true
}).then(function(data) {
postHeaders['If-Match'] = "";
res.send(data).end()
console.log("All done!");
})
})
原文地址:https://codeday.me/bug/20190622/1265770.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。