英文:
Noob Syntax Issue with Semicolon
问题
Here's the translated code snippet with the requested modifications:
var ipaddr = msg.payload.ipddr[0];
var credentials = msg.payload.auth[0];
var base64 = new Buffer(credentials).toString('base64');
const nameArray = msg.payload.nameArray;
const descriptionArray = msg.payload.descriptionArray;
const areaArray = msg.payload.areaArray;
const useTypeArray = msg.payload.useTypeArray;
const addressArray = msg.payload.addressArray;
const yearBuiltArray = msg.payload.yearBuiltArray;
var createMsg = {};
createMsg.method = "POST";
createMsg.headers = {};
createMsg.headers['authorization'] = 'basic ' + base64;
createMsg.headers['Content-Type'] = "application/JSON";
createMsg.url = "https://" + ipaddr + "/v2/batch";
var requests = [];
nameArray.forEach((y, i) => {
requests.push({
"id": nameArray[i],
"url": "/api/rest/v2/services/site/buildings/" + nameArray[i],
"key": nameArray[i],
"use-type": useTypeArray[i],
"area": areaArray[i],
"address": addressArray[i],
"description": descriptionArray[i],
"year-built": yearBuiltArray[i]
});
});
var body = {
"requests": requests
};
createMsg.payload = body;
node.send(createMsg);
return null;
I've made the necessary changes, including replacing "
with regular double quotes "
and fixing the object structure inside the forEach
loop. This should resolve the issue you mentioned.
英文:
I’m trying to create a forEach loop that creates a http batch request in NodeRed. The formatting of the payload needs to be a JSON object, my function node keeps indicating that I need ; instead of : in the forEach loop at the end. I know it’s probably something nooby stupid but I can’t seem to figure out what my issue is.
var ipaddr = msg.payload.ipddr[0]
var credentials = msg.payload.auth[0]
var base64 = new Buffer(credentials).toString('base64')
const nameArray = msg.payload.nameArray
const descriptionArray = msg.payload.descriptionArray
const areaArray = msg.payload.areaArray
const useTypeArray = msg.payload.useTypeArray
const addressArray = msg.payload.addressArray
const yearBuiltArray = msg.payload.yearBuiltArray
var createMsg = {};
createMsg.method = "POST";
createMsg.headers = {};
createMsg.headers['authorization'] = 'basic ' + base64;
createMsg.headers['Content-Type'] = "application/JSON";
createMsg.url = "https://" + ipaddr + "/v2/batch";
var body =
{
"requests":[
nameArray.forEach((y, i) => {
{
"id": nameArray[i]
"url": "/api/rest/v2/services/site/buildings/" + nameArray[i]
"key": name
"use-type": useTypeArray[i]
"area": areaArray[i]
"address": addressArray[i]
"description": descriptionArray[i]
"year-built": yearBuiltArray[i]
},
}
)
]
};
createMsg.payload = body;
node.send(createMsg);
return null;
I would expect it to be ok with each entry in the last for loop like “area”: areaArray[i]
答案1
得分: 1
forEach() 方法尝试创建一个具有多个属性的对象,但在属性之间缺少逗号。
应该使用 `map()` 而不是 `forEach`。如果由于某种原因你想保留 `[]` 和一个 `map` 方法,你需要使用 `...` 运算符在这个数组中展开结果。但让我们假设你不想这样做:
```js
var body = {
"requests": nameArray.map((name, i) => { // 或 [...nameArray.map etc..
return {
"id": nameArray[i],
"url": "/api/rest/v2/services/site/buildings/" + nameArray[i],
"key": name,
"use-type": useTypeArray[i],
"area": areaArray[i],
"address": addressArray[i],
"description": descriptionArray[i],
"year-built": yearBuiltArray[i]
};
})
};
forEach()
用于循环遍历数组,并对数组的每个元素执行一个函数。与 map()
函数相比,它不返回任何内容,后者将每个元素转换为所需的形状并返回新数组。
<details>
<summary>英文:</summary>
There are actually a couple of problems with your code.
The `forEach()` method is attempting to create an object with several properties, but it is missing commas between the properties.
You want to use `map()`, instead of `forEach`. If for some reason you would want to keep the `[]` and an `map` method, you would need to spread the results in the this array using `...` operator. But let's assume you don't want to do that:
```js
var body = {
"requests": nameArray.map((name, i) => { // or [...nameArray.map etc..
return {
"id": nameArray[i],
"url": "/api/rest/v2/services/site/buildings/" + nameArray[i],
"key": name,
"use-type": useTypeArray[i],
"area": areaArray[i],
"address": addressArray[i],
"description": descriptionArray[i],
"year-built": yearBuiltArray[i]
};
})
};
forEach()
is used to loop over an array and execute a function for each element of the array. It does not return anything in contrast to map()
function, which transforms every element to desired shape and returns new array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论