从 mongoose 实体解析 JSON
Posted
技术标签:
【中文标题】从 mongoose 实体解析 JSON【英文标题】:parse JSON from the mongoose entity 【发布时间】:2015-04-18 22:32:57 【问题描述】:我有以下条目:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ProjectTemplateSchema = new Schema(
title:
type: String
,
connectionsString:
type: String
,
creator:
type: Schema.ObjectId,
ref: 'User'
)
mongoose.model('ProjectTemplate', ProjectTemplateSchema)
当用户请求项目模板列表时,我会渲染一个 ejs 页面:
exports.renderTemplate = function(req, res)
ProjectTemplate.find().sort('creator').populate('creator', 'firstName lastName fullName').exec(function(err, projectTemplates)
if (err)
return res.status(400).send(
message: getErrorMessage(err)
)
else
//TODO: NOT WORKING
res.render('projectTemplate',
title: 'project template page',
user: JSON.stringify(req.user),
projectTemplates: JSON.stringify(projectTemplates)
)
)
然后在 ejs 文件中,我简单地创建一个变量来存储 json 字符串
<% include common/header %>
<% include common/canvas %>
<script type="text/javascript" src="/public/projectTemplate/projectTemplate.client.module.js"></script>
<script type="text/javascript" src="/public/projectTemplate/projectTemplate.client.controller.js"></script>
<script>
var projectTemplates = "<%- projectTemplates %>"
</script>
<% include common/footer %>
在客户端,我得到了这个结果:
var projectTemplates = "["_id":"54dc83fc5c2c43bcf371580c","connectionsString":"[]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"farhser","__v":0,"_id":"54e14ad2ec168f7c724d7c94","connectionsString":"[]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"asdfqwe","__v":0,"_id":"54dc186b4db5b64654ed20b3","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project b","__v":0,"_id":"54dc3a995cb1efb17729ba14","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"dfiweoif3","__v":0,"_id":"54dc1780d87343b3fe78e27a","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project a","__v":0,"_id":"54dc2dbc8ea46d216bf5b19e","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":1,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":1,\"targetTaskIndex\":3]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project c","__v":0,"_id":"54dc3d74577fa3167bc7962f","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"projd","__v":0,"_id":"54dc3f88993a2a3b7d48515f","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project f","__v":0,"_id":"54dc4021e3c1fd0a7ef51de3","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project g","__v":0,"_id":"54dc48d68eec924986764c16","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"proj h","__v":0,"_id":"54dc586ab68a07d89ab4a395","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project z","__v":0,"_id":"54dc595987802b4b9c07a9dd","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"bsdf","__v":0,"_id":"54dc5ad7f8735d119ffb05b5","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"bfgfn","__v":0,"_id":"54dc5b784fca6552a17e3ff5","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"nfdgh","__v":0,"_id":"54dc651a95f19d97be10f32e","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"bafd","__v":0,"_id":"54dc80185c2c43bcf3715804","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project 2","__v":0,"_id":"54dc845b016dfd41f868c4a2","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"sgjsthe","__v":0,"_id":"54e192b70c8c3565b19fcacb","connectionsString":"","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"qherwe","__v":0,"_id":"54e14ae0ec168f7c724d7c96","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"fawe","__v":0,"_id":"54e14ed9726d809b767fa011","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"safaew","__v":0,"_id":"54e1bb1903d30d63d54790e6","connectionsString":"","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"fahwrewe","__v":0,"_id":"54e1bb8670c551ddd5239389","connectionsString":"","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"hjfyjt","__v":0,"_id":"54e1c1a29ada1c7bd914121b","connectionsString":"[\"sourceTaskIndex\":1,\"targetTaskIndex\":0]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"hfgtjdr","__v":0,"_id":"54e1c5e6a3c63bbee0a21ce7","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"fjdtj","__v":0,"_id":"54e1c62799377201e1b87850","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"arhsrt","__v":0,"_id":"54e1c7f5f03fdbb9e26ced04","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"dhehert","__v":0,"_id":"54e1cbf4e041fb00e7da2b1f","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"proja","__v":0]"
如果我尝试<%= %>
,我有这个:
var projectTemplates = "["_id":"54dc83fc5c2c43bcf371580c","connectionsString":"[]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"farhser","__v":0,"_id":"54e14ad2ec168f7c724d7c94","connectionsString":"[]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"asdfqwe","__v":0,"_id":"54dc186b4db5b64654ed20b3","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project b","__v":0,"_id":"54dc3a995cb1efb17729ba14","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"dfiweoif3","__v":0,"_id":"54dc1780d87343b3fe78e27a","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project a","__v":0,"_id":"54dc2dbc8ea46d216bf5b19e","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":1,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":1,\"targetTaskIndex\":3]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project c","__v":0,"_id":"54dc3d74577fa3167bc7962f","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"projd","__v":0,"_id":"54dc3f88993a2a3b7d48515f","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project f","__v":0,"_id":"54dc4021e3c1fd0a7ef51de3","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project g","__v":0,"_id":"54dc48d68eec924986764c16","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"proj h","__v":0,"_id":"54dc586ab68a07d89ab4a395","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2,\"sourceTaskIndex\":2,\"targetTaskIndex\":3]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project z","__v":0,"_id":"54dc595987802b4b9c07a9dd","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"bsdf","__v":0,"_id":"54dc5ad7f8735d119ffb05b5","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"bfgfn","__v":0,"_id":"54dc5b784fca6552a17e3ff5","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"nfdgh","__v":0,"_id":"54dc651a95f19d97be10f32e","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"bafd","__v":0,"_id":"54dc80185c2c43bcf3715804","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"project 2","__v":0,"_id":"54dc845b016dfd41f868c4a2","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1,\"sourceTaskIndex\":2,\"targetTaskIndex\":3,\"sourceTaskIndex\":0,\"targetTaskIndex\":2]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"sgjsthe","__v":0,"_id":"54e192b70c8c3565b19fcacb","connectionsString":"","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"qherwe","__v":0,"_id":"54e14ae0ec168f7c724d7c96","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"fawe","__v":0,"_id":"54e14ed9726d809b767fa011","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"safaew","__v":0,"_id":"54e1bb1903d30d63d54790e6","connectionsString":"","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"fahwrewe","__v":0,"_id":"54e1bb8670c551ddd5239389","connectionsString":"","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"hjfyjt","__v":0,"_id":"54e1c1a29ada1c7bd914121b","connectionsString":"[\"sourceTaskIndex\":1,\"targetTaskIndex\":0]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"hfgtjdr","__v":0,"_id":"54e1c5e6a3c63bbee0a21ce7","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"fjdtj","__v":0,"_id":"54e1c62799377201e1b87850","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"arhsrt","__v":0,"_id":"54e1c7f5f03fdbb9e26ced04","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"dhehert","__v":0,"_id":"54e1cbf4e041fb00e7da2b1f","connectionsString":"[\"sourceTaskIndex\":0,\"targetTaskIndex\":1]","creator":"_id":"54dc1771d87343b3fe78e279","fullName":"undefined undefined","id":"54dc1771d87343b3fe78e279","title":"proja","__v":0]"
两个json都坏了。当我尝试解析 JSON 时应用程序崩溃
angular.module('projectTemplate').controller('ProjectTemplateController', ['$scope', '$routeParams', '$location', 'Authentication', 'ProjectTemplate',
function($scope, $routeParams, $location, Authentication, ProjectTemplate)
$scope.authentication = Authentication;
//TODO: NOT WORKING!!!
$scope.projectTemplates = JSON.parse(projectTemplates)
【问题讨论】:
检查$query.populate(..)的语法,我觉得有问题。 你通常可以解析出 "但问题在于您的连接字符串已经是 JSON 字符串化字符串,因此当您再次对其进行字符串化时,您会得到:\"e;哪个javascript不喜欢。 【参考方案1】:由于您使用的是 Angular,您可以尝试通过 ngResource 传递变量
ProjectTemplate.find().sort('creator').populate('creator', 'firstName lastName fullName').exec(function(err, projectTemplates)
if (err)
return res.status(400).send(
message: getErrorMessage(err)
)
else
projectTemplates = projectTemplates.map(function(projectTemplate)
return projectTemplate.toObject();
)
res.json(projectTemplates)
)
在你的角度控制器的开头,这样做
$scope.projectTemplates = ProjectTemplate.query()
别忘了添加 ProjectTemplate 资源/服务
projectTemplate.factory('ProjectTemplate', ['$resource', function($resource)
return $resource('/projectTemplate/api/:projectTemplateId',
projectTemplateId: '@_id'
,
update:
method: 'PUT'
)
])
稍微扭曲一下你的服务器路由,将 projectTemplate 数据与视图模板的渲染分开请求(这是一个更好的设计)
【讨论】:
它有效。我认为问题在于猫鼬模型的 toObject() 而不是 toJSON()以上是关于从 mongoose 实体解析 JSON的主要内容,如果未能解决你的问题,请参考以下文章
使用 Mongoose 和 GraphQL 保存实体后数据为空