[英]How do I send the result of a function to a third one without a return?
我試圖出於單元測試的目的在NodeJS服務中調用Cloudant映射函數。
我已經創建了自己的發射函數,但是仍然無法獲得返回所需信息的視圖。
const data = {
"_id": "68e3d2807c0faa169d504068db99e03f",
"_rev": "2-c3b3249a2eb090e87dc4c053712691c3",
"type": "user",
"name": "Whatever",
"email": "Whatever@google.com",
"cn": "5517605X",
"phone": null,
"deleted": true
}
const emit = function (arg1, arg2) {
return { key: arg1, value: arg2 };
};
var map = function (doc) {
if (doc.type && doc.type === 'user') {
emit([doc.cn, doc.deleted], doc);
}
};
const result = map(data);
console.log(result);
JSF只是我要嘗試做的事情。 我知道我可以通過在emit函數前面添加一個返回值來解決此問題,但是由於要從Cloudant直接獲取視圖,因此我無法編輯map函數。
有什么辦法可以使我繼續工作嗎?
但我無法編輯地圖功能
這樣就無法更改map
函數的返回值。 調用它將始終導致undefined
。 除了更改map
(或使用其他方法)之外,別無他法。
在特定的情況下,你已經證明, emit
可以關閉了result
,並直接設置它,如果你移動的result
宣布了上述emit
,並使它成為let
:
let result;
const emit = function (arg1, arg2) {
result = { key: arg1, value: arg2 };
return result;
};
var map = function (doc) {
if (doc.type && doc.type === 'user') {
emit([doc.cn, doc.deleted], doc);
}
};
map(data);
console.log(result);
...但這是特定於該代碼的,即使您不追求全面的功能編程方法,擁有類似副作用的功能也不是一件好事。 但有時需要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.