想输出用户的ID值并在控制台上显示

时间:2016-12-09 01:47:11

标签: node.js mapping sails.js waterline onesignal

我正在为应用程序使用sailsjs框架。我正在构建,我正在尝试从我拥有的模型中提取id值:

const memoryCreatorId = _(Memory.creator).map('id').value();
console.log(memoryCreatorId);
   const message = {
        app_id: '***********************',
        contents: {"en": "Yeah Buddy, Rolling Like a Big Shot!"},
        filters: [{"field": "tag", "key": "userId", "relation": "=", "value": memoryCreatorId}],
        ios_badgeType: 'Increase',
        ios_badgeCount: 1
      };
    return PushNotificationService.sendNotification(message);

我基本上试图获得Memory.creator.User.id值。所以基本上是创建记忆的人的用户ID。我试图从" Memory" model" creator"属性,映射到"用户"模型,并从"用户"模型,提取" id"属性。感谢您的帮助!

以下内存模型:

Memory.js
const _ = require('lodash');

module.exports = {
  attributes: {
    creator: {
      model: 'User'
    },
    title: {
      type: 'string'
    },
    description: {
      type: 'text'
    },
    contentUrl: {
      type: 'string',
      url: true
    },
    cropRect: {
      type: 'string'
    },
    likers: {
      collection: 'User',
      via: 'memoryLikes'
    },
    comments: {
      collection: 'Comment',
      via: 'memory'
    },
    update: {
      model: 'Update',
    },
    cause: {
      model: 'Cause',
    }
  }
};

用户模型如下:

User.js
'use strict';

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

const CipherService = require('../services/CipherService');
const BraintreeService = require('../services/BraintreeService');

module.exports = {
  attributes: {
    id: {
      type: 'string',
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
      unique: true,
      index: true,
      uuidv4: true
    },
    firstName: {
      type: 'string',
      defaultsTo: ''
    },
    lastName: {
      type: 'string',
      defaultsTo: ''
    },
    email: {
      type: 'string',
      email: true,
      required: true,
      unique: true
    },
    password: {
      type: 'string'
    },
    passwordResetToken: {
      type: 'string'
    },
    passwordResetTokenExpires: {
      type: 'string'
    },
    type: {
      type: 'string',
      enum: ['admin', 'member']
    },
    city: {
      type: 'string'
    },
    state: {
      type: 'string'
    },
    address: {
      type: 'string'
    },
    institution: {
      model: 'Institution'
    },
    major: {
      type: 'string'
    },
    contentUrl: {
      type: 'string',
      url: true,
      defaultsTo: AwsService.getAssetImageUrl('user-default.png')
    },
    cropRect: {
      type: 'string'
    },
    graduationYear: {
      type: 'integer'
    },
    donations: {
      collection: 'Donation',
      via: 'donor'
    },
    memories: {
      collection: 'Memory',
      via: 'creator'
    },
    causes: {
      collection: 'Cause',
      via: 'followers',
      dominant: true
    },
    adminCauses: {
      collection: 'Cause',
      via: 'admins'
    },
    isLeader: {
      type: 'boolean',
      defaultsTo: false
    },
    isCurrentStudent: {
      type: 'boolean',
      defaultsTo: false
    },
    isAdmin: {
      type: 'boolean',
      defaultsTo: false
    },
    adminTitle: {
      type: 'string'
    },
    paymentProfile: {
      model: 'PaymentProfile'
    },
    jsonWebTokens: {
      collection: 'Jwt',
      via: 'owner'
    },
    memoryLikes: {
      collection: 'Memory',
      via: 'likers'
    },
    updateLikes: {
      collection: 'Update',
      via: 'likers'
    },
    toJSON: function() {
      return User.clean(this);
    }
  },
  beforeUpdate: (values, next) => {
    CipherService.hashPassword(values).then(() => next()).catch(next);
  },
  beforeCreate: (values, next) => {
    CipherService.hashPassword(values).then(() => next()).catch(next);
  },
  clean: (user) => {
    //let obj = user.toObject();

    delete user.password;
    delete user.jsonWebTokens;
    delete user.passwordResetToken;
    delete user.passwordResetTokenExpires;

    return user;
  }
};

0 个答案:

没有答案