嗨,我想合并两个集合并发送合并的结果。我做了以下。我正在使用mongoclient。
> module.exports = function (context, req) {
>
> //making the connection
> var response;
> //check for the request method
>
> if (req.method !== 'GET') {
> response = {
> status: 500,
> body: {
> "code": 500,
> "message": "Invalid request. Please check the request method or parameter"
> },
> headers: {
> 'Content-Type': 'application/json'
> }
> }
> context.done(null, response);
> return;
> } //end of if
>
> mongoClient.connect(dbUrl, function (err, client) {
> assert.equal(null, err);
> var database = client.db(databaseConfig.mLabDbName);
> var response;
> var getUserIdParams = req.query.userId
> var index = req.query.index;
> var noOfResults = mapResultsWithIndex(index)
>
>
> getSubByUId(database, context, getUserIdParams, function (res, err) {
>
> // console.log("response from the callback", res)
>
>
> getForms(database, context, res, function (result) {
> console.log("response from secondcallback", result)
>
>
> //closing the connection
> client.close();
>
> if (err) {
> console.log("an error in getting the result");
> response = {
> status: 500,
> body: {
> code: 500,
> message: "Error in getting the forms"
> },
> headers: {
> 'Content-Type': 'application/json'
> }
> }
> } //end of error
> else {
> if (result.length > 0) {
> console.log("got the data");
> response = {
> status: 200,
> body: {
> code: 200,
> result: result
> },
> headers: {
> 'Content-Type': 'application/json'
> }
> }
> } else {
> console.log("did not get the data");
> response = {
> status: 204,
> body: {
> code: 204,
> message: "No submissions found for the user id"
> },
> headers: {
> 'Content-Type': 'application/json'
> }
> }
> }
>
> }
>
> context.done(null, response)
> })
>
>
> }) //end of subId
>
> }) //closing mongodb connection }; //closing function
>
我的主要功能是:
var getForms = function (db, context, array, callback) {
let newArray = [];
// console.log("array uuuuu", array)
for (let i = 0; i < array.length; i++) {
db.collection('forms').find({_id:ObjectID(array[i].formId)}).toArray(function (err, res) {
console.log("rskkkk",res.length)
res.forEach(doc => {
newArray.push({
"subId": array[i]._id,
"formId": array[i].formId,
"userId": array[i].userId,
"formDescription": array[i].formDescription,
"formFields": array[i].formFields,
"createdAt": array[i].createdAt,
"data": array[i].data,
"imageformedarray": doc.imageUploadedarray
})
})
callback(newArray);
console.log("new array is", newArray.length);
})
}
}
var getSubByUId = function (db, context, getUserIdParams, callback) {
let arr = [],
type;
var getSubmissions = db.collection('user-submission').find({
userId: getUserIdParams
});
getSubmissions.toArray(function (err, res) {
res.forEach(doc => {
var value = doc.createdAt
if (doc.hasOwnProperty("updatedAt")) {
value = doc.updatedAt
}
if ((typeof (doc.formFields)) === "string") {
console.log("string")
type = JSON.parse(doc.formFields)
// type = doc.data().formFields
} else {
type = doc.formFields
}
arr.push({
"subId": doc._id,
"formId": doc.formId,
"userId": doc.userId,
"formDescription": doc.formDescription,
"formFields": type,
"createdAt": value,
"data": doc
})
});
// console.log("arr is", arr);
callback(arr, err)
})
}
我想返回整个数组,但是它只在第一个循环中返回结果。当我将存在该 getForms 的回调移动到那里时,它将返回具有空值的结果。 请帮忙。
答案 0 :(得分:0)
使用for循环迭代数组时,将触发getForms
方法的回调。这会将控制权传递给回调,然后该回调将处理将始终输出第一个元素的函数的context.done
。
按原样,此代码还将导致对context.done的多次调用,每个函数执行仅应调用一次。
请记住,context.done的定义是context.done([err],[propertyBag])
,它期望将对象作为输出。
有关更多参考,请检查:https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node
除了将functions.json
输出绑定设置为:
{
"type": "http",
"direction": "out",
"name": "$return"
}
现在,如果将callback(newArray)
移到for loop
之外,您应该会看到预期的结果。