使用javascript模板文本外推变量的正确语法?
有人能告诉我关于模板文本的正确语法的正确方向吗 我有以下代码(没有模板文本,工作正常): 现在我想把使用javascript模板文本外推变量的正确语法?,javascript,template-literals,extrapolation,Javascript,Template Literals,Extrapolation,有人能告诉我关于模板文本的正确语法的正确方向吗 我有以下代码(没有模板文本,工作正常): 现在我想把useNewUrlParser:true放在一个变量中: const dbUrl = 'localhost:27017/imgManager', dbOptions = 'useNewUrlParser: true'; mongoose.connect('mongodb://' + dbUrl, { dbOptions }); 当然不行,mongoose.connect()抱怨它没有
useNewUrlParser:true
放在一个变量中:
const dbUrl = 'localhost:27017/imgManager',
dbOptions = 'useNewUrlParser: true';
mongoose.connect('mongodb://' + dbUrl, { dbOptions });
当然不行,mongoose.connect()
抱怨它没有dbOptions
:
$ node server.js
Server up: http://localhost:3300
the options [dbOptions] is not supported
我想模板文字是最好的选择,但正确的语法是什么?我尝试了以下方法,但均无效:
`mongoose.connect('mongodb://' + dbUrl, { ${dbOptions} });`
mongoose.connect(`mongodb://${dbUrl}, { ${dbOptions} }`);
mongoose.connect(`mongodb:\/\/${dbUrl}, { ${dbOptions} }`);
有什么想法吗?因为
dbOptions
在您的方法中是一个字符串,这个{dbOptions}
正在创建一个js对象,如下所示:
// cause that mongoose is complaining because it's not expecting
// options with property name dbOptions
{ dbOptions: 'useNewUrlParser: true' }
您应该执行以下操作
const dbUrl = 'localhost:27017/imgManager',
dbOptions = {useNewUrlParser: 'true'};
// `mongodb://${dbUrl}` will generate the following string:
// "mongodb://localhost:27017/imgManager"
mongoose.connect(`mongodb://${dbUrl}`, dbOptions);
由于
dbOptions
在您的方法中是一个字符串,因此该{dbOptions}
正在创建一个js对象,如下所示:
// cause that mongoose is complaining because it's not expecting
// options with property name dbOptions
{ dbOptions: 'useNewUrlParser: true' }
您应该执行以下操作
const dbUrl = 'localhost:27017/imgManager',
dbOptions = {useNewUrlParser: 'true'};
// `mongodb://${dbUrl}` will generate the following string:
// "mongodb://localhost:27017/imgManager"
mongoose.connect(`mongodb://${dbUrl}`, dbOptions);
模板文本不能在任意位置工作,也不能创建任意的JS语法。(不带标记)它们只创建字符串,并且只创建一个值,而不会同时创建两个参数。作为选项,您需要传递一个对象:
const dbUrl = 'localhost:27017/imgManager',
dbOptions = { useNewUrlParser: true };
mongoose.connect(
`mongodb://${ dbUrl }`, // first argument
dbOptions // second argument
);
如果您确实将选项作为字符串获取,则需要将其解析为对象,例如:
const dbUrl = 'localhost:27017/imgManager',
dbOptions = '"useNewUrlParser": true';
mongoose.connect(
`mongodb://${ dbUrl }`, // first argument
JSON.parse(`{${ dbOptions }}`) // second argument. The string is '{' + dbOptions + '}'
);
模板文本不能在任意位置工作,也不能创建任意的JS语法。(不带标记)它们只创建字符串,并且只创建一个值,而不会同时创建两个参数。作为选项,您需要传递一个对象:
const dbUrl = 'localhost:27017/imgManager',
dbOptions = { useNewUrlParser: true };
mongoose.connect(
`mongodb://${ dbUrl }`, // first argument
dbOptions // second argument
);
如果您确实将选项作为字符串获取,则需要将其解析为对象,例如:
const dbUrl = 'localhost:27017/imgManager',
dbOptions = '"useNewUrlParser": true';
mongoose.connect(
`mongodb://${ dbUrl }`, // first argument
JSON.parse(`{${ dbOptions }}`) // second argument. The string is '{' + dbOptions + '}'
);
模板文字仅用于字符串,而不是对象
dbOptions
应该是对象,而不是字符串
const dbOptions = { useNewUrlParser: true };
然后使用变量本身:
mongoose.connect('mongodb://' + dbUrl, dbOptions);
模板文字仅用于字符串,而不是对象
dbOptions
应该是对象,而不是字符串
const dbOptions = { useNewUrlParser: true };
然后使用变量本身:
mongoose.connect('mongodb://' + dbUrl, dbOptions);
为什么您希望将所有参数作为一个字符串,并以某种方式期望该方法读取该字符串并解释参数是什么?我看得越多,就越觉得它是一个-我不认为您的查询与模板文本有任何关系,也不认为使用它们的解决方案。如果要将
dbOptions
存储为变量,可以执行dbOptions={useNewUrlParser:true}
,然后调用mongoose.connect('mongodb://'+dbUrl,dbOptions)代码>模板文字用于创建文本,而不是代码。为什么您希望将所有参数作为单个字符串,并以某种方式期望该方法读取该字符串并解释参数是什么?我看得越多,越觉得这是一个-我不认为您的查询与模板文字有任何关系,也不是使用它们的解决方案。如果要将dbOptions
存储为变量,可以执行dbOptions={useNewUrlParser:true}
,然后调用mongoose.connect('mongodb://'+dbUrl,dbOptions)代码>模板文本用于创建文本,而不是代码。当然dbOptions
应该是一个对象,{useNewUrlParser:true}
当然dbOptions
应该是一个对象,{useNewUrlParser:true}
谢谢!很好!谢谢很好!