【问题标题】:datastore - sets a primary key to one of the property fields数据存储 - 将主键设置为属性字段之一
【发布时间】:2018-09-08 02:44:49
【问题描述】:

我在我的应用程序中使用gstore-node

const gstore = require('gstore-node')();

const { Schema } = gstore;

const userSchema = new Schema({
  name: { type: String },
  userId: String,
});

我想让userId 成为我的key。所以我可以做类似的事情

const userId = '1234';
userSchema.get(userId)

我试过了:

  const entityKey = UserSchema.key(userId);
  const user = new UserSchema({ key: entityKey });

但是

 UserSchema.get(userId); // Not Found

我怎样才能做到这一点?

【问题讨论】:

    标签: google-cloud-datastore datastore


    【解决方案1】:

    想通了。实体创建中的第二个参数是关键 所以 const user = new UserSchema({name: 'Homer'}, userId);

    这使得userId 成为键,因此我可以使用get 函数查找用户

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 1970-01-01
      • 2010-11-22
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多