小编kum*_*arD的帖子

如何保存从mongoose中的query.exec()函数返回的对象

我是猫鼬的新手.这是我的场景:

var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]});
var Parent = mongoose.model('Parent', parentSchema);
Run Code Online (Sandbox Code Playgroud)

假设我已经创建了一个带有子项的父级'p',我正在查询'p',使用

var query = Parent.find({"_id":"562676a04787a98217d1c81e"});
query.select('children');                                   
query.exec(function(err,person){                            
    if(err){                                                    
        return console.error(err);                               
    } else {                                                     
        console.log(person);                                     
    }
});                                                    
Run Code Online (Sandbox Code Playgroud)

我需要访问异步函数之外的person对象.有关如何做到这一点的任何想法?

mongoose mongodb node.js

4
推荐指数
1
解决办法
736
查看次数

回调不是mongoose.find({})中的函数

我是Node.js和mongoose的新手,我试图使用find({})从mongo集合中查询对象,函数如下:

schema.statics.listAllQuizes = function listAllQuizes(){
Model.find({},function(err,quizes,cb){
    if(err){
        return cb(err);
    }else if(!quizes){
        return cb();
    }
    else {
        return cb(err,quizes);
    }
});};
Run Code Online (Sandbox Code Playgroud)

但是,当我调用此函数时,我得到一个错误说

        return cb(err,quizes);
               ^
        TypeError: cb is not a function
Run Code Online (Sandbox Code Playgroud)

我被困在这一点上,有人可以帮我解决这个问题,提前谢谢.

javascript mongoose mongodb node.js

3
推荐指数
1
解决办法
4509
查看次数

错误:mongoose-auto-increment尚未初始化

我正在创建一个mongoose模式如下:

var adminSchema = new mongoose.Schema({
email:{type:String,unique:true,required:true},
passwordHash:{type:String},
isActivated:{type:Boolean},
admin_id:{type:Number,unique:true}
});

adminSchema.plugin(autoIncrement.plugin(),{model:'adminSchema',
field:'admin_id',
startAt:1,
incrementBy:1});
Run Code Online (Sandbox Code Playgroud)

当我尝试运行该应用程序时,我收到一个错误说:

 if (!counterSchema || !IdentityCounter) throw new Error("mongoose-auto-increment has not been initialized");
                                      ^

Error: mongoose-auto-increment has not been initialized
at Object.exports.plugin (/Users/kumar/WebstormProjects/code-master/node_modules/mongoose-auto-increment/index.js:36:49)
at Object.<anonymous> (/Users/kumar/WebstormProjects/code-master/models/admin.js:22:34)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.global.App.require (/Users/kumar/WebstormProjects/code-master/app.js:23:16)
at Object.global.App.model (/Users/kumar/WebstormProjects/code-master/app.js:32:21)
at init (/Users/kumar/WebstormProjects/code-master/app/initializers/passport.js:6:15)
at Object.<anonymous> (/Users/kumar/WebstormProjects/code-master/app.js:66:44)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js mongoose-plugins

3
推荐指数
1
解决办法
4184
查看次数

如何使用jvmti获取方法局部变量和类变量的值

我试图使用 JVMTI 捕获变量值,当生成异常事件时,我浏览了 jvmti 文档,发现没有函数可以让我检索字段(变量)的值,如何实现?

下面是代理代码:

#include<jni.h>
#include<jvmti.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct {
jvmtiEnv *jvmti;
jrawMonitorID lock;
} GlobalAgentData;

static GlobalAgentData *gdata;

static bool check_jvmti_error(jvmtiEnv *jvmti,jvmtiError errnum,const char *str){
if(errnum != JVMTI_ERROR_NONE){
    char *errnum_str;
    errnum_str = NULL;
    (void)(*jvmti)->GetErrorName(jvmti,errnum,&errnum_str);
    printf("ERROR: JVMTI: %d(%s): %s\n", errnum, 
    (errnum_str==NULL?"Unknown":errnum_str),
    (str==NULL?"":str));
    return false;
   }
   return true;
 }

static void deallocate(jvmtiEnv *jvmti,void *ptr){
jvmtiError error;
error = (*jvmti)->Deallocate(jvmti,ptr);
check_jvmti_error(jvmti,error,"Cannot deallocate memory");
}

static void allocate(jvmtiEnv *jvmti,jint len){
jvmtiError error;
void *ptr;
error = (*jvmti)->Allocate(jvmti,len,(unsigned char **)&ptr); …
Run Code Online (Sandbox Code Playgroud)

java java-native-interface jvmti

3
推荐指数
1
解决办法
1899
查看次数

使用 GORM golang 保留自定义设置数据类型

我在 go 中创建了一个自定义Set数据类型,我用它来定义一对多关系。例如,在我的架构中,我有以下结构定义

type Doctor struct {
  firstName string
  lastName string
  capabilities commons.Set
 }
Run Code Online (Sandbox Code Playgroud)

capabilities是一组具有以下值的字符串chat, audio, video,通过此设置,我尝试将上述结构保留到MySQL使用GORM库中,但是当我这样做时,出现以下错误

panic: invalid sql type Set (interface) for mysql

goroutine 6 [running]:
catalog/vendor/github.com/jinzhu/gorm.(*mysql).DataTypeOf(0xc00027e8a0, 0xc00024d680, 0x8, 0x8)
    /home/kumard/go/src/catalog/vendor/github.com/jinzhu/gorm/dialect_mysql.go:123 +0xce9
catalog/vendor/github.com/jinzhu/gorm.(*Scope).createTable(0xc000169400, 0xc14e60)
Run Code Online (Sandbox Code Playgroud)

我知道我必须实现某些方法才能实现此目的,但我无法弄清楚此处要实现哪个方法/回调。

ThreadUnsafeSet 定义:

type threadUnsafeSet map[interface{}]struct{}    

type OrderedPair struct {
    First interface{}
    Second interface{}
}    

func newThreadUnsafeSet() threadUnsafeSet {
    return make(threadUnsafeSet)
}    

func (pair *OrderedPair) Equal(other OrderedPair) bool {
    return pair.First == other.First && …
Run Code Online (Sandbox Code Playgroud)

go go-gorm

3
推荐指数
1
解决办法
5745
查看次数