Javascript 使用ES6保存函数mongoose
我想知道在没有外部库的情况下,是否有可能在ES6中实现这种行为。我引用了.save函数,因为在尝试这样做时,我遇到了一个错误,它说无法读取未定义的属性“saveInfo”Javascript 使用ES6保存函数mongoose,javascript,mongoose,ecmascript-6,Javascript,Mongoose,Ecmascript 6,我想知道在没有外部库的情况下,是否有可能在ES6中实现这种行为。我引用了.save函数,因为在尝试这样做时,我遇到了一个错误,它说无法读取未定义的属性“saveInfo” schema.statics.saveInfo = function(info,callback){ var toSet = new Info(info); // I want this save on es6 toSet.save(function(err){ if
schema.statics.saveInfo = function(info,callback){
var toSet = new Info(info);
// I want this save on es6
toSet.save(function(err){
if (err){
return handleError(err);
}else{
callback("Success!...");
}
}); }
module.exports=Info=mongoose.model'userInfo',schema
这在es6中是这样的
schema.statics.saveInfo = (info, callback) => {
const toSet = new Info(info);
toSet.save(err => {
if (err){
return handleError(err);
}else{
callback("Success!...");
}
});
导出默认信息=mongoose.model'userInfo',schema;
但是我得到一个错误,说信息是未定义的。问题是要使用导出默认值,我必须先创建变量,然后才能导出它 所以我改变了我的代码 导出默认信息=mongoose.model'userInfo',schema 为此: const Info=mongoose.model'userInfo',UserSchema;
导出默认信息 ECMAScript 2015是向后兼容的,所以在我看来,您尝试使用了一个新功能,但没有成功。请更具体地说明什么不起作用。另外,最后一行是无效语法。您的意思是传递您的模式,而不是模式吗?还有,什么是toSet,为什么不使用Save?您在ES5中使用了它吗?请给我们看看那个密码。否则,这实际上与ES6无关-您的代码甚至没有使用任何新的ES6特性。