async for loop in node.js

I’ve reduced your code sample to the following lines to make it easier to understand the explanation of the concept. var results = []; var config = JSON.parse(queries); for (var key in config) { var query = config[key].query; search(query, function(result) { results.push(result); }); } res.writeHead( … ); res.end(results); The problem with the previous code is … Read more

Mongoose, CastError: Cast to Array failed for value when trying to save a model that contains a model

Man, I had a similar issue creating an Schema like this: QuestionnaireSchema = mongoose.Schema({ formId: Number, name: String, questions: [ { type: String, title: String, alternatives:[{ label: String, value: “Mixed” }] } ] }); My mistake was that I am using “type” as a field name and this is reserved word in mongoose. I just … Read more

node dotenv won’t work with pm2

dotenv will read .env file located in the current directory. When you call pm2 start myapp/app.js it won’t search for myapp/.env. .env // It will try to load this, which doesn’t exist myapp/ app.js So you have two solutions use path option: const path = require(‘path’); require(‘dotenv’).config({ path: path.join(__dirname, ‘.env’) }); Or call your script … Read more