填充sails js返回null

时间:2017-03-25 20:08:41

标签: node.js sails.js waterline populate

我有2个模型用户和user_weapon

module.exports = {
tableName: 'user',
autoCreatedAt: false,
autoUpdatedAt: false,
  attributes: {
	  Id: {
      type: 'integer',
      unique: true,
      primaryKey: true,
	  autoIncrement: true,
      columnName: 'id'
    },
	nickName: {
      type: 'string',
      columnName: 'nickname'
    },
    userName: {
      type: 'string',
      columnName: 'username'
    },
	password: {
      type: 'string',
      columnName: 'password'
    },
	level: {
      type: 'integer',
      columnName: 'level'
    },
	point: {
      type: 'integer',
      columnName: 'point'
    },
	coin: {
      type: 'integer',
      columnName: 'coin'
    },
	heros: {
      type: 'integer',
      columnName: 'heros_id'
    },
	status: {
      type: 'string',
      columnName: 'status'
    }
	,
	userweapon: {
     collection: 'user_weapon',
      via: 'user'
    }

  }
};

和User_weapon.js

/**
* User_weapon.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {
tableName: 'user_weapon',
autoCreatedAt: false,
autoUpdatedAt: false,
  attributes: {
	  Id: {
      type: 'integer',
      unique: true,
      primaryKey: true,
	  autoIncrement: true,
      columnName: 'id'
    },
    weaponLevelId: {
      type: 'integer',
      columnName: 'weaponlevel_id'
    },
	  weaponId: {
      type: 'integer',
      columnName: 'weapon_id'
    },
	user: {
		 columnName: 'user_id',
	  model: 'users'
    }
	
	
	

  }
};

当我搜索用户并使用User_weapon结果填充用户模型时

[
  {
    "userweapon": [],
    "password": "1",
    "level": 0,
    "point": 0,
    "coin": 0,
    "status": "online",
    "Id": 3,
    "nickName": "گالا",
    "userName": "1",
    "heros": 1
  }
]

为什么我的userweapon为null但在数据库中它是空的 我的User_weapon表是enter image description here

1 个答案:

答案 0 :(得分:0)

在User_weapon.js中:

 user: {
         columnName: 'user_id',
         model: 'user'
    }

模型的值应该是用户而不是用户

首先创建User条目并获取User ID并创建user_weapon模型,或者如果已经有user_weapon条目,则使用user_weapon.js的用户columnName中的用户ID更新它;

[
  {
    "userweapon": [
      {
        "Id": 3,
        "weaponLevelId": "25",
        "weaponId": "2",
        "user": 4
      },
      {
        "Id": 4,
        "weaponLevelId": "4",
        "weaponId": "3",
        "user": 4
      }
    ],
    "password": "12345",
    "level": "0",
    "coin": "$",
    "status": "Active",
    "Id": 4,
    "nickName": "priyan",
    "userName": "Anand",
    "heros": 25
  },
  {
    "userweapon": [],
    "password": "12345",
    "level": "0",
    "coin": "$",
    "status": "Active",
    "Id": 5,
    "nickName": "mogu",
    "userName": "Mohan",
    "heros": 25
  }
]