Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何让这个嵌套模式在Mongoose中工作?_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何让这个嵌套模式在Mongoose中工作?

Node.js 如何让这个嵌套模式在Mongoose中工作?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,因此,我试图找出如何在命令列表中保存多个命令,但我尝试过的所有方法都没有奏效。到目前为止,我是这样设置的,但当它保存时,它会以 "command_list" : [ { "action" : "goto,goto", "target" : "http://www.google.com,http://www.cnn.com" } ] 当我真的想要 "command_list" : [ "command" : { "action" : "goto", "target" : "http://www.

因此,我试图找出如何在命令列表中保存多个命令,但我尝试过的所有方法都没有奏效。到目前为止,我是这样设置的,但当它保存时,它会以

"command_list" : [ { "action" : "goto,goto", "target" : "http://www.google.com,http://www.cnn.com" } ]
当我真的想要

"command_list" : [ "command" : { "action" : "goto", "target" : "http://www.google.com" },                     
                   "command" : { "action" : "goto", "target" : "http://www.cnn.com" } ]
其中有多个命令。到目前为止,我的app.js是这样存储数据的

var configSample = new Configurations({
        command_list_size: request.body.command_list_size,
        command_list: [ {action: request.body.action, target: request.body.target}] 
});
var mongoose = require("mongoose");

var command = mongoose.Schema({
    action: String,
    target: String
});

var configSchema = mongoose.Schema({
    command_list_size: Number,
    command_list: [command] 
});


module.exports = mongoose.model('Configurations', configSchema);
模型是这样的

var configSample = new Configurations({
        command_list_size: request.body.command_list_size,
        command_list: [ {action: request.body.action, target: request.body.target}] 
});
var mongoose = require("mongoose");

var command = mongoose.Schema({
    action: String,
    target: String
});

var configSchema = mongoose.Schema({
    command_list_size: Number,
    command_list: [command] 
});


module.exports = mongoose.model('Configurations', configSchema);

那么,我该如何进行嵌套操作呢?谢谢

将数据发送到服务器时,似乎没有正确打包数据。如果您使用以下选项:

command_list: [ {action: request.body.action, target: request.body.target}]
它将抓住所有的动作,将它们组合在一起,并对目标执行相同的操作。您最好向服务器发送一个数组,其中已经嵌套了文档

另一种选择是在服务器上接收到数据后解析数据以提取元素,但我认为首先将其打包会更容易

补充:

如果要拆分现有对象,可以使用String.split()方法重新生成对象:

// not certain the chaining will work like this, but you get the idea. It works
// on the string values you'll receive
var actions = response.body.action.split(',');
var targets = response.body.target.split(',');

// the Underscore library provides some good tools to manipulate what we have
// combined the actions and targets arrays
var combinedData = _.zip(actions, targets);

// go through the combinedData array and create an object with the correct keys
var commandList = _.map(combinedData, function(value) { 
    return _.object(["action", "target"], value)
});
可能有更好的方法来创建新对象,但这确实有效

编辑:

我提出了一个关于尝试的问题