Mongoose Populate Cast错误

时间:2016-12-15 19:44:06

标签: node.js mongodb

我有以下方案:

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

class CustomDialog : public QDialog
{
public:
    CustomDialog(const QStringList& items)
    {
        setLayout(new QHBoxLayout());

        box = new QComboBox;
        box->addItems(items);
        layout()->addWidget(box);
        connect(box, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(colorSelected(const QString&)));
        QPushButton* ok = new QPushButton("ok");
        layout()->addWidget(ok);
        connect(ok, &QPushButton::clicked, this, [this]()
        {
           accept();
        });
    }

    QComboBox* combobox() { return box; }

private:
    QComboBox* box;
};

void Dialog::on_pushButton_clicked()
{
    QStringList itemList({"Red", "Blue", "Green"});
    CustomDialog dialog(itemList);
   // connect(box, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(colorSelected(const QString&)));
    if (dialog.exec() == QDialog::Accepted)
    {

        scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);
        QBrush redBrush(Qt::red);
        QBrush blackBrush(Qt::black);
        QPen blackpen(Qt::black);
        blackpen.setWidth(3);
        rectangle = scene->addRect(10,10,100,100,blackpen,redBrush);
        rectangle->setFlag(QGraphicsItem::ItemIsMovable);
    }
}

void Dialog::colorSelected(const QString& text)
{
      const QColor selected =  colorMap[text];
}

我想在线搜索用户ID,然后打印用户名,不确定我做错了什么。 这是我得到的错误:

// online.js
var mongoose = require('mongoose');

// Online Friends
var onlineSchema = mongoose.Schema({
  userid:{ type: Number, ref:'Player' }
},{timestamps : true});


// Export

module.exports = mongoose.model('Online', onlineSchema);


//player.js
var mongoose = require('mongoose');

// define the schema for player data
var playerSchema = mongoose.Schema({
  userid    : { type: Number, required: true,unique:true, default: 0 },
  firstname    : { type: String },
  nick : {type: String, required: true},
  lastname : {type: String},
  lastupdate: {type: Date, default: Date.now}
},{timestamps : true});


// create the model and expose it to our app
module.exports = mongoose.model('Player', playerSchema);


//main.js
...
const Online = require('./config/models/online.js');
const Player = require('./config/models/player.js');

Online.find({userid:3441341}).populate('userid').exec(function(err,result){
  if (err) {
    console.log(err);
  } else {
    console.log(result);
  }
});

...

实现这一目标的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

将用户ID类型从Number更改为mongoose.Schema.ObjectId,它应该有效。它基本上表示您正在尝试将数字转换为Object Id。将id作为字符串传递。 在NoSQL DBS中,您的文档必须具有_id字段。您有用户模型还是使用播放器来完成工作?如果您将Player用作用户模型,请将user_id更改为_id