【问题标题】:SyntaxError: Missing initializer in destructuring declarationSyntaxError:解构声明中缺少初始化程序
【发布时间】:2017-05-01 20:23:08
【问题描述】:

我在 Node.js 版本 6.9.5 上运行它

我有这个代码:

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

理论上是以下的简化版本:

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

我收到此错误:

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

我尝试了这个:

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

我得到一个不同的错误,这是“mongoose”未定义的结果。

我认为可以做这样的事情,我做错了什么?

【问题讨论】:

  • 没有。 let {Schema}, mongoose = require('mongoose');let {Schema}; let mongoose = require('mongoose'); 相同,所以它不起作用。 let mongoose, {Schema} = require('mongoose');let mongoose; let {Schema} = require('mongoose'); 相同,所以 mongoose 确实是未定义的。
  • 谢谢,你能把这个添加为答案吗,很难读。
  • 对不起,我已经添加了。

标签: javascript node.js ecmascript-6


【解决方案1】:

没有。

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

和这个一样

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

所以它不会起作用,因为它不存在从中获取 Schema 的对象。

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

和这个一样

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

mongoose 确实是未定义的。

【讨论】:

    【解决方案2】:

    对我来说,这是因为我返回的是空变量,所以我必须检查它们的值。确保返回正确的数据。

    【讨论】: