Reputation: 6469
I tried to make api request twice and the first one work, but the second fails with below error.
{
"name": "GeneralError",
"message": "this.app.service(...).create(...) is not a function",
"code": 500,
"className": "general-error",
"data": {},
"errors": {}
}
result.class.js
/* eslint-disable no-unused-vars */
const errors = require('@feathersjs/errors');
exports.Submission = class Submission {
constructor (options, app) {
this.app = app;
}
async create (data, params) {
if (Array.isArray(data)) {
return Promise.all(data.map(current => this.create(current, params)));
}
let result = await this.app.service('result').create({ //<------------- It works
sectionId: data.sectionId,
})
console.log(result) // <------------------------------------ able to show the value
(data.sub_sections).map(async sub_section =>{
(sub_section.questions).map(async question =>{
let payload = {
answerId: question.questionId
}
await this.app.service('result').create(payload) //<------------- It results in error
})
})
return data;
}
};
Upvotes: 3
Views: 683
Reputation: 3020
It doesn't seem that it is the second call of this.app.service().create()
that is causing the error you're seeing.
I am guessing the specific error message reported occurs when you do not have the console.log()
statement in there. This is because of a missing semi-colon.
let result = await this.app.service('result').create({
sectionId: data.sectionId,
}) // no semi-colon here
(data.sub_sections).map(//..)
JavaScript only treats a line break as a semicolon - ie an end of statement - if the next nonspace character cannot be interpreted as a continuation of the current statement. But in this case, it can - as an invocation of the result of the preceding await
expression.
Note that when inserting the console.log()
statement in between, you should encounter a similar issue:
console.log(...) is not a function
So just add a semi colon manually in this case.
Upvotes: 3