Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Missing initializer in destructuring declaration

I am running this on Node.js version 6.9.5

I have this code:

let {Schema}, mongoose = require('mongoose');

which is in theory a simplified version of:

let mongoose = require('mongoose');
let Schema = mongoose.Schema;

I get this error:

let {Schema}, mongoose = require('mongoose');
    ^^^^^^^^
SyntaxError: Missing initializer in destructuring declaration

I tried this instead:

let mongoose, {Schema} = require('mongoose');

I got a different error, which was the result of "mongoose" being undefined.

I thought it was possible to do something like this, what am I doing wrong?

like image 700
Alexander Mills Avatar asked May 01 '17 20:05

Alexander Mills


2 Answers

No.

let {Schema}, mongoose = require('mongoose');

it's same as

let {Schema};
let mongoose = require('mongoose');`

so it will not work because it's not exists object wherefrom take Schema .

let mongoose, {Schema} = require('mongoose');

it's same as

let mongoose;
let {Schema} = require('mongoose');`

And mongoose is really undefined.

like image 56
rie Avatar answered Oct 26 '22 23:10

rie


For me it was because I was returning empty variables so I had to check their values. Make sure you're returning the right data.

like image 34
Merunas Grincalaitis Avatar answered Oct 26 '22 23:10

Merunas Grincalaitis